色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php實(shí)現(xiàn)mysql同步的實(shí)現(xiàn)方法

拿到需求之后,發(fā)現(xiàn)這兩個(gè)網(wǎng)站的MYSQL數(shù)據(jù)庫(kù)都不能遠(yuǎn)程訪問(wèn)(安全第一吧)。于是想起了 平時(shí)使用的CSV文件批量錄入數(shù)據(jù)。于是
嘗試使用CSV導(dǎo)入導(dǎo)出。
導(dǎo)入到處框架如下:
1首先將數(shù)據(jù)導(dǎo)出成CSV的格式。
建立一文件,放置在中國(guó)服務(wù)器上:csv.php.其實(shí)就是一個(gè)導(dǎo)出函數(shù),通過(guò)數(shù)據(jù)庫(kù),表名和SQL語(yǔ)句來(lái)獲得數(shù)據(jù)。
csv.php
復(fù)制代碼 代碼如下:
<?php
/**
* 輸出一個(gè)數(shù)據(jù)庫(kù)中的表到一個(gè)CSV文件中
*
* @param string Mysql數(shù)據(jù)庫(kù)的主機(jī)
* @param string 數(shù)據(jù)庫(kù)名稱(chēng)
* @param string 數(shù)據(jù)庫(kù)中的表名
* @param string 數(shù)據(jù)庫(kù)的連接用戶名
* @param string 數(shù)據(jù)庫(kù)的連接密碼
* @param string 數(shù)據(jù)庫(kù)的表名
* @param string 數(shù)據(jù)庫(kù)的
* @param string 錯(cuò)誤頁(yè)面
* @param string SQL語(yǔ)句
*
* @return text 返回CSV格式的內(nèi)容
*
* @access public
*/
function PMA_exportData(host,db,user,pass,filename,table, crlf, error_url, sql_query) {
what="csv";
csv_terminated=" ";
csv_separator=",";
csv_enclosed=" ";
csv_escaped=" ";
mysql_connect(host, user,pass) or die("不能連接數(shù)據(jù)庫(kù),錯(cuò)誤代碼如下:" . mysql_error());
mysql_select_db(db);
result = mysql_query(sql_query);
fields_cnt = mysql_num_fields(result);
cc="";
//fp = fopen(filename, 'w');
// 格式化數(shù)據(jù)
while (row = mysql_fetch_row(result)) {
schema_insert = '';
for (j = 0; j < fields_cnt; j++) {
if (!isset(row[j]) || is_null(row[j])) {
schema_insert .="NULL"; //用什么來(lái)替換空值
} elseif (row[j] == '0' || row[j] != '') {
// loic1 :用引號(hào)包含字段值
if (csv_enclosed == '') {
schema_insert .= row[j];
} else {
schema_insert .= csv_enclosed
. str_replace(csv_enclosed, csv_escaped . csv_enclosed, row[j])
. csv_enclosed;
}
} else {
schema_insert .= '';
}
if (j < fields_cnt-1) {
schema_insert .= csv_separator;
}
} // end for
// fwrite(fp,schema_insert . csv_terminated);
cc.=schema_insert . csv_terminated;
} // end while
mysql_free_result(result);
// fclose(fp);
return cc;
}
?>

2.將CSV格式的內(nèi)容導(dǎo)入到表中
在美國(guó)服務(wù)器上建立個(gè)導(dǎo)入的文件,放置:import.php ,代碼如下:
復(fù)制代碼 代碼如下:
<?php
/**
* 從一個(gè)上傳的文件中將數(shù)據(jù)導(dǎo)入到一個(gè)表中
*
* @param string Mysql數(shù)據(jù)庫(kù)的主機(jī)
* @param string 數(shù)據(jù)庫(kù)名稱(chēng)
* @param string 數(shù)據(jù)庫(kù)中的表名
* @param string 數(shù)據(jù)庫(kù)的連接用戶名
* @param string 數(shù)據(jù)庫(kù)的連接密碼
* @param string 數(shù)據(jù)庫(kù)的表名
*
* @return bool 是否執(zhí)行成功
*
* @access public
*/
function uploadFileOfCsv(host,db,user,pass,table,content){
mysql_connect(host, user,pass) or die("不能連接數(shù)據(jù)庫(kù),錯(cuò)誤代碼如下:" . mysql_error());
mysql_select_db(db);
result = mysql_query("select * from table");
fields_cnt = mysql_num_fields(result);
test2=array(array());
rownum=0;
log("提取的數(shù)據(jù)如下:<br>".content);
fd1 = fopen ("C:test.csv",'a');
fwrite(fd1,content);
fclose(fd1);
fp = fopen("C:test.csv", "r");
while (buffer = fgets(fp,4096))
{
i++;
tmp_arr = explode(",",buffer);
if(trim(tmp_arr[0]) == ""){
echo "<script language='Javascript'>";
echo "alert('第".i."行的ID空,請(qǐng)檢查!');";
echo "location.href=document.referrer;";
echo "</script>";
exit;
}
query = "INSERT INTO db.table";
query .=" values ( ";
for(q=0;q<fields_cnt;q++){
if(q==fields_cnt-1){
tmp=tmp_arr[q];
query.="'tmp');";
}else{
tmp=tmp_arr[q];
query.="'tmp',";
}
}//end for(q=0;
log2(query);
mysql_query(query);
}
fclose(fp);
return "OK";
unlink("C:test.csv");
}
function log2(event = null){
//global db;
// global login;
if(LOG_ENABLED){
now = date("Y-M-d H:i:s");
fd = fopen ("C:log.html",'a');
log = now." "._SERVER["REMOTE_ADDR"] ." - event <br>";
fwrite(fd,log);
fclose(fd);
}
}
?>

3調(diào)用函數(shù)執(zhí)行導(dǎo)出
在中國(guó)服務(wù)器上再建立一個(gè) 文件:test_export.php,調(diào)用前面的csv.php的函數(shù),然后將數(shù)據(jù)轉(zhuǎn)成CSV,然后臨時(shí)存到一個(gè)表單的
textera中,注意表單提交的位置:
復(fù)制代碼 代碼如下:
<?php
require_once("csv.php");
host="localhost";
db="project";
user="root";
pass="";
//導(dǎo)出tb_contact表的數(shù)據(jù)為csv文件
filename = 'file4.csv';
cc=PMA_exportData( host,db,user,pass, filename,"tb_project_dvp", "", "test.php", "select * from tb_project_dvp") ;
handle = fopen(filename, "rb");
contents = fread(handle, filesize (filename));
fclose(handle);
?>
<form id="form1" name="form1" method="post" action="http://美國(guó)網(wǎng)站的地址/test2.php">
<p>
<textarea name="textarea" cols="180" rows="30"><?php echo cc?></textarea>
<input type="hidden" name="action" value="1"/>
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>

再在美國(guó)服務(wù)器上防置如下文件用于接受上傳上來(lái)的數(shù)據(jù),文件名為 test_import.php
復(fù)制代碼 代碼如下:
<?php
require_once("csv.php");
require_once("import.php");
host="localhost";
db="wintopweb";
user="root";
pass="";
if(_POST['action']=="1"){
content=_POST['textarea'];
echo uploadFileOfCsv(host,db,user,pass,"tb_project_dvp",content);
}
?>

最后 利用Windows-xp/nt/03 控制面版中自帶 任務(wù)計(jì)劃,調(diào)度執(zhí)行中國(guó)服務(wù)器test_export.php文件即可

php技術(shù)php實(shí)現(xiàn)mysql同步的實(shí)現(xiàn)方法,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 富裕县| 桦川县| 布尔津县| 东海县| 阿拉尔市| 内黄县| 公安县| 方正县| 来安县| 利津县| 盈江县| 梁山县| 喀什市| 门头沟区| 长垣县| 高邮市| 樟树市| 德保县| 望都县| 泰来县| 宕昌县| 那曲县| 江门市| 齐齐哈尔市| 周口市| 嘉义县| 井陉县| 新兴县| 凉山| 邳州市| 叙永县| 忻城县| 介休市| 佛冈县| 安平县| 巴青县| 高台县| 宣恩县| 河源市| 安国市| 黔东|