欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

php語言

PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql的方法

時(shí)間:2025-05-28 10:50:16 php語言 我要投稿
  • 相關(guān)推薦

PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql的方法

  由于公司的英文網(wǎng)站放置在美國,而這些網(wǎng)站的數(shù)據(jù)要與大陸的服務(wù)器數(shù)據(jù)同步。 同步時(shí)間在一天之內(nèi)。下面是小編為大家?guī)淼腜HP實(shí)現(xiàn)同步遠(yuǎn)程Mysql的方法,歡迎閱讀。

  PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql:

  需求:由于公司的英文網(wǎng)站放置在美國,而這些網(wǎng)站的數(shù)據(jù)要與大陸的服務(wù)器數(shù)據(jù)同步。 同步時(shí)間在一天之內(nèi)。

  拿到需求之后,發(fā)現(xiàn)這兩個(gè)網(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í)就是一個(gè)導(dǎo)出函數(shù),通過數(shù)據(jù)庫,表名和SQL語句來獲得數(shù)據(jù)。

  <?php

  /**

  * 輸出一個(gè)數(shù)據(jù)庫中的表到一個(gè)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 錯(cuò)誤頁面

  * @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="&nbsp;";

  mysql_connect(host, user,pass) or die("不能連接數(shù)據(jù)庫,錯(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_ = '';

  for (j = 0; j < fields_cnt; j++) {

  if (!isset(row[j]) || is_null(row[j])) {

  schema_ .="NULL"; //用什么來替換空值

  } elseif (row[j] == '0' || row[j] != '') {

  // loic1 :用引號包含字段值

  if (csv_enclosed == '') {

  schema_ .= row[j];

  } else {

  schema_ .= csv_enclosed

  . str_replace(csv_enclosed, csv_escaped . csv_enclosed, row[j])

  . csv_enclosed;

  }

  } else {

  schema_ .= '';

  }

  if (j < fields_cnt-1) {

  schema_ .= csv_separator;

  }

  } // end for

  // fwrite(fp,schema_ . csv_terminated);

  cc.=schema_ . csv_terminated;

  } // end while

  mysql_free_result(result);

  // fclose(fp);

  return cc;

  }

  ?>

  2.將CSV格式的內(nèi)容導(dǎo)入到表中

  在美國服務(wù)器上建立個(gè)導(dǎo)入的文件,放置:import.php ,代碼如下:

  <?php

  /**

  * 從一個(gè)上傳的文件中將數(shù)據(jù)導(dǎo)入到一個(gè)表中

  *

  * @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ù)庫,錯(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空,請檢查!');";

  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ù)器上再建立一個(gè) 文件:test_export.php,調(diào)用前面的`csv.php的函數(shù),然后將數(shù)據(jù)轉(zhuǎn)成CSV,然后臨時(shí)存到一個(gè)表單的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í)現(xiàn)同步遠(yuǎn)程Mysql的方法】相關(guān)文章:

PHP常用MySql操作的方法10-11

php+mysql實(shí)現(xiàn)無限分類實(shí)例詳解07-23

PHP常用MySql操作方法06-09

PHP讀取MySQL數(shù)據(jù)的代碼方法10-21

PHP讀取MySQL數(shù)據(jù)代碼方法08-29

PHP 中 MySQL 數(shù)據(jù)庫異步查詢實(shí)現(xiàn)08-22

WEB中使用PHP連接MySQL的方法09-27

php頁面緩存實(shí)現(xiàn)方法07-20

PHP實(shí)現(xiàn)多線程的方法08-02