Happy source -PHP achieve the export to MySQL database instance .sql file

  1. To obtain the first database table in which, as a function mysql_list_tables ({OA credit disc erection q <319.135.503.1>}), may then be obtained all the table names stored to an array.
  2. show create table table table structure can be obtained.
  3. select * from table remove all records, with stitched loop insert into ... statement.

Implementation code:
Copy the code code is as follows:
<PHP?

header("Content-type:text/html;charset=utf-8");

//配置信息
$cfg_dbhost = "localhost";
$cfg_dbname = "ftdm";
$cfg_dbuser = "root";
$cfg_dbpwd = "root";
$cfg_db_language = "utf8";
$to_file_name = "ftdm.sql";
// END 配置

// links database
$ Link = mysql_connect ($ cfg_dbhost, cfg_dbuser $, $ cfg_dbpwd);
the mysql_select_db ($ cfg_dbname);
// selective encoding
mysql_query ( "set names" $ cfg_db_language .);
Which database has tables //
$ tables = mysql_list_tables ($ cfg_dbname);
// table to record such an array
$ TabList = array ();
the while ($ Row = mysql_fetch_row ($ tables)) {
$ TabList [] = $ Row [0];
}

echo "operation, a Please wait ...";
$ = info "- --------------------------- - ";
$ = info." - date: "dATE. (" Ymd H: I: S ";", Time ()). "
$ info =" - only for testing and study, the present program does not. super suitable for handling large amounts of data ";
$ info =." - ---------------------------- ";
file_put_contents ($ to_file_name, $ info, FILE_APPEND);

// the table structure of each table to a file export
the foreach (AS $ $ TabList Val) {
$ SQL = "Create Table Show" $ Val;.
$ RES = the mysql_query (SQL $, $ Link);
$ Row = the mysql_fetch_array ( RES $);
$ info = "- ----------------------------";
. $ info = "- Structure for the Table ".$val."";
$ info =." - ---------------------------- ";
. $ info =" DROP TABLE IF EXISTS ".$val."; " ;
$ sqlstr Row = $ $ info [. 1].. ";";
// appended to the file
file_put_contents (to_file_name $, $ sqlstr, FILE_APPEND to);
// release resources
mysql_free_result ($ RES);
}

// table for each export data files to
the foreach (AS $ $ TabList Val) {
$ = SQL "SELECT * from" $ Val;.
$ RES = the mysql_query (SQL $, $ Link);
// if there is no table data, proceed to the next table an
IF (mysql_num_rows ($ RES) <. 1) continue;
//
$ info = "- ---------------------- ------ ";
. $ info =" - Records for ".$val."";
$ info =." - ------------------------ ---- ";
file_put_contents (to_file_name $, $ info, FILE_APPEND to);
// read the data
the while ($ Row = mysql_fetch_row ($ RES)) {
$ sqlstr =" the INSERT the INTO ".$val."the VALUES ( ";
the foreach (AS $ $ Row ZD) {
. sqlstr $ = "". "ZD $." ",";
}
// remove the last comma and a space
$ sqlstr = substr ($ sqlstr, 0, strlen ($ sqlstr) -2);
$ sqlstr. = ");";
file_put_contents($to_file_name,$sqlStr,FILE_APPEND);
}
//释放资源
mysql_free_result($res);
file_put_contents($to_file_name," ",FILE_APPEND);
}

echo "OK!";

?>

Guess you like

Origin blog.51cto.com/14524882/2435891