<?PHPHeader("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 Encoding mysql_query("Set names".)$cfg _db_language); //What tables are in the database $tables=Mysql_list_tables($cfg _dbname); //log 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. = "-For testing and learning only, this program is not suitable for handling 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); //Freeing Resources Mysql_free_result($res); } //export data from 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); //reading 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); } //Freeing Resources Mysql_free_result($res); file_put_contents($to _file_name, "\ r \ n",file_append); } Echo"Ok!";?>
Http://www.jb51.net/article/49971.htm
Database backup with PHP code can make the management of the site very convenient, we can directly into the background operation to complete the database backup.
Key technologies:
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.
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.
ThinkPHP3.2 Implementing MySQL Database backup