MuSQL以檔案形式匯入匯出整個資料庫

來源:互聯網
上載者:User

MuSQL以檔案形式匯入匯出整個資料庫

MuSQL以檔案形式匯入匯出整個資料庫 :
<?php/* 匯出SELECT * INTO OUTFILE 'c:/name.txt'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\''LINES TERMINATED BY '\n'FROM zones;匯入LOAD DATA INFILE 'c:/name.txt' INTO TABLE zones  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\''LINES TERMINATED BY '\n';*/define(HOST,'127.0.0.1');define(USER,'user');define(PWD,'pwd');define(DBNAME,'dbname');define(DIR,'C:/xampp/htdocs/backup/');//1為匯入 0為匯出define(OPERATION,1);mysql_connect(HOST,USER,PWD) ordie("Could not connect: " . mysql_error());mysql_select_db(DBNAME) ordie("Could not select db: " . mysql_error());$result = mysql_query("show tables");if(!is_dir(DIR)){die('Folder does not exist');}if(OPERATION){while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$fileName=DIR.$row[0].".txt";if(file_exists($fileName)){$querySql='TRUNCATE TABLE `'.$row[0].'`';mysql_query($querySql) ordie("Could not truncate table: ". $querySql . mysql_error());$querySql = "LOAD DATA INFILE '".$fileName."' INTO TABLE `".$row[0]."`FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\''LINES TERMINATED BY '\n'";mysql_query($querySql) ordie("Could not insert into table: ". $querySql . mysql_error());echo 'insert into table '.$row[0].' success.<br/>';}else{echo 'Not insert into table '.$row[0].'<br/>';}}echo "The task is finished";}else{if (is_writable(DIR)) {while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$fileName=DIR.$row[0].".txt";if(file_exists($fileName)){unlink($fileName);}$querySql = "SELECT * INTO OUTFILE '".$fileName."'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\''LINES TERMINATED BY '\n' FROM `".$row[0]."`";mysql_query($querySql) ordie("Could not dump table: ". $querySql . mysql_error());echo 'dump table '.$row[0].' success.<br/>'; }echo "The task is finished";}else{echo 'Folder '.DIR.' can not writeable';}}mysql_free_result($result);mysql_close();?>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.