Using PHP to export MySQL database backup to SQL sample _php tutorial

Source: Internet
Author: User
A good use of PHP to export MySQL database backup to SQL example, I hope it will be helpful to everyone.

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.

Function:


SQL statement effect derived from

Specific code:

The code is as follows Copy Code

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 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 ...
";

$info = "------------------------------RN";

$info. = "--Date:". Date ("Y-m-d h:i:s", Time ()). " RN ";

$info. = "--Power by Degary Blog (http://www.daixiaorui.com/read/34.html) rn";

$info. = "-For testing and learning only, this procedure is not suitable for handling super-large data RN";

$info. = "------------------------------rnrn";

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 = "------------------------------RN";

$info. = "--Table structure for '". $val. " ' RN ';

$info. = "------------------------------RN";

$info. = "DROP TABLE IF EXISTS '". $val. " '; Rn ';

$SQLSTR = $info. $row [1]. "; Rnrn ";

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 = "------------------------------RN";

$info. = "--Records for '". $val. " ' RN ';

$info. = "------------------------------RN";

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. = "); RN";

File_put_contents ($to _file_name, $SQLSTR, file_append);

}

Freeing resources

Mysql_free_result ($res);

File_put_contents ($to _file_name, "RN", file_append);

}

echo "ok!";

?>

http://www.bkjia.com/PHPjc/632913.html www.bkjia.com true http://www.bkjia.com/PHPjc/632913.html techarticle a good use of PHP to export MySQL database backup to SQL example, I hope it will be helpful to everyone. Using PHP code to implement database backup can make the management of the website very convenient, I ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.