<?php
header("Content-type:text/html;charset=utf-8");
//Configuration information
$cfg_dbhost = 'localhost';
$cfg_dbname = 'ftdm';
$cfg_dbuser = 'root';
$cfg_dbpwd = 'root';
$cfg_db_language = 'utf8';
$to_file_name = "";
// END configuration
//Link database
$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//Select encoding
mysql_query("set names ".$cfg_db_language);
//What tables are there in the database
$tables = mysql_list_tables($cfg_dbname);
//Record these tables to an array
$tabList = array();
while($row = mysql_fetch_row($tables)){
$tabList[] = $row[0];
}
echo "In operation, please wait patiently...<br/>";
$info = "-- ----------------------------\r\n";
$info .= "-- Date: ".date("Y-m-d H:i:s",time())."\r\n";
$info .= "-- It is only used for testing and learning, this program is not suitable for processing too much data\r\n";
$info .= "-- ----------------------------\r\n\r\n";
file_put_contents($to_file_name,$info,FILE_APPEND);
//Export the table structure of each table to the file
foreach($tabList as $val){
$sql = "show create table ".$val;
$res = mysql_query($sql,$link);
$row = mysql_fetch_array($res);
$info = "-- ----------------------------\r\n";
$info .= "-- Table structure for `".$val."`\r\n";
$info .= "-- ----------------------------\r\n";
$info .= "DROP TABLE IF EXISTS `".$val."`;\r\n";
$sqlStr = $info.$row[1].";\r\n\r\n";
//Add to file
file_put_contents($to_file_name,$sqlStr,FILE_APPEND);
//Release resources
mysql_free_result($res);
}
//Export the data of each table to a file
foreach($tabList as $val){
$sql = "select * from ".$val;
$res = mysql_query($sql,$link);
//If there is no data in the table, continue to the next table
if(mysql_num_rows($res)<1) continue;
//
$info = "-- ----------------------------\r\n";
$info .= "-- Records for `".$val."`\r\n";
$info .= "-- ----------------------------\r\n";
file_put_contents($to_file_name,$info,FILE_APPEND);
//Read data
while($row = mysql_fetch_row($res)){
$sqlStr = "INSERT INTO `".$val."` VALUES (";
foreach($row as $zd){
$sqlStr .= "'".$zd."', ";
}
// Remove the last comma and space
$sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);
$sqlStr .= ");\r\n";
file_put_contents($to_file_name,$sqlStr,FILE_APPEND);
}
//Release resources
mysql_free_result($res);
file_put_contents($to_file_name,"\r\n",FILE_APPEND);
}
echo "OK!";
?>