淺析PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql_PHP教程
推薦:PHP session常見問題集錦及解決辦法1. 錯誤提示 Warning: Cannot send session cookie - headers already sent Warning: Cannot send session cache limiter - headers already sent 分析及解決辦法 這一類問題,的原因是你在程序中使用session_start()時(shí),之前已經(jīng)有實(shí)際的html內(nèi)容輸出了。或
需求:由于公司的英文網(wǎng)站放置在美國,而這些網(wǎng)站的數(shù)據(jù)要與大陸的服務(wù)器數(shù)據(jù)同步。 同步時(shí)間在一天之內(nèi)。
拿到需求之后,發(fā)現(xiàn)這兩個網(wǎng)站的MYSQL數(shù)據(jù)庫都不能遠(yuǎn)程訪問(安全第一吧)。于是想起了 平時(shí)使用的CSV文件批量錄入數(shù)據(jù)。于是嘗試使用CSV導(dǎo)入導(dǎo)出。
導(dǎo)入到處框架如下:
1首先將數(shù)據(jù)導(dǎo)出成CSV的格式。
建立一文件,放置在中國服務(wù)器上:csv.php.其實(shí)就是一個導(dǎo)出函數(shù),通過數(shù)據(jù)庫,表名和SQL語句來獲得數(shù)據(jù)。
<?php
/**
* 輸出一個數(shù)據(jù)庫中的表到一個CSV文件中
*
* @param string Mysql數(shù)據(jù)庫的主機(jī)
* @param string 數(shù)據(jù)庫名稱
* @param string 數(shù)據(jù)庫中的表名
* @param string 數(shù)據(jù)庫的連接用戶名
* @param string 數(shù)據(jù)庫的連接密碼
* @param string 數(shù)據(jù)庫的表名
* @param string 數(shù)據(jù)庫的
* @param string 錯誤頁面
* @param string SQL語句
*
* @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ù)庫,錯誤代碼如下:" . 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"; //用什么來替換空值
} elseif (row[j] == '0' || row[j] != '') {
// loic1 :用引號包含字段值
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)入到表中
在美國服務(wù)器上建立個導(dǎo)入的文件,放置:import.php ,代碼如下:
<?php
/**
* 從一個上傳的文件中將數(shù)據(jù)導(dǎo)入到一個表中
*
* @param string Mysql數(shù)據(jù)庫的主機(jī)
* @param string 數(shù)據(jù)庫名稱
* @param string 數(shù)據(jù)庫中的表名
* @param string 數(shù)據(jù)庫的連接用戶名
* @param string 數(shù)據(jù)庫的連接密碼
* @param string 數(shù)據(jù)庫的表名
*
* @return bool 是否執(zhí)行成功
*
* @access public
*/
function uploadFileOfCsv(host,db,user,pass,table,content){
mysql_connect(host, user,pass) or die("不能連接數(shù)據(jù)庫,錯誤代碼如下:" . 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空,請檢查!');";
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)出
在中國服務(wù)器上再建立一個 文件:test_export.php,調(diào)用前面的csv.php的函數(shù),然后將數(shù)據(jù)轉(zhuǎn)成CSV,然后臨時(shí)存到一個表單的textera中,注意表單提交的位置:
<?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://美國網(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>
再在美國服務(wù)器上防置如下文件用于接受上傳上來的數(shù)據(jù),文件名為 test_import.php:
<?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í)行中國服務(wù)器test_export.php文件即可
分享:PHP實(shí)例:生成靜態(tài)頁面的函數(shù)?php function CreateShtml() { ob_start(callback_CteateShtml); } function callback_CteateShtml(buffer) { page = intval(@_REQUEST[page]); //fileName = _SERVER['DOCUMENT_ROOT'] . dirname(_SERVER['PHP_SELF']) . /article/ . basename(_SERV
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎概率的抽獎算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-淺析PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql。