1. First to get the tables in the database, the function mysql_list_tables (), and then you can save all the table names obtained to an array. ----------------This function is replaced with show tables from database_name because it is deprecated
2. The Show create table table name can be used to get the tables structure.
3. Select * FROM table name to take out all records, with loop stitching into the insert into ... Statement.
<?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 = "Ftdm.sql"; END Configuration//link Database $link = mysql_connect ($cfg _dbhost, $cfg _dbuser, $cfg _dbpwd); mysql_select_db ($cfg _dbname); Select the encoding mysql_query ("Set names". $cfg _db_language); What tables are in the database//$tables = Mysql_list_tables ($cfg _dbname);
$query = "Show tables from $cfg _dbname";
$tables = mysql_query ($query, $link);
Log these tables to an array $tabList = array (); while ($row = Mysql_fetch_row ($tables)) {$tabList [] = $row [0];} echo "In operation, please be patient ...<br/>"; $info = "------------------------------\ r \ n"; $info. = "--Date:". Date ("Y-m-d h:i:s", Time ()). " \ r \ n "; $info. = "-For testing and learning only, this program is not suitable for processing super-large amounts of 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 a 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 "; Append to File file_put_contents ($to _file_name, $SQLSTR, file_append); Release Resource Mysql_free_result ($res); }//Export data for each table to 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!"; ?>
PHP implementation to export MySQL database as a. sql file instance (imitation phpMyAdmin export function)