Php export. SQL file/mysql database backup program _ PHP Tutorial

Source: Internet
Author: User
Php exports the. SQL file mysql database backup program. Php Tutorial export. SQL file mysql tutorial database tutorial backup program? Php $ database; database name $ optionsarray (hostname, IP address charsetutf8, encoding filename php Tutorial export. SQL file/mysql tutorial database tutorial backup program

$ Database = ''; // database name
$ Options = array (
'Hostname' => '', // IP address
'Charset' => 'utf8', // encoding
'Filename' => $ database. '. SQL', // file name
'Username' => '',
'Password' =>''
);
Mysql_connect ($ options ['hostname'], $ options ['username'], $ options ['password']) or die ("The database cannot be connected! ");
Mysql_select_db ($ database) or die ("The database name is incorrect! ");
Mysql_query ("set names '{$ options ['charset']}'");
$ Tables = list_tables ($ database );
$ Filename = sprintf ($ options ['filename'], $ database );
$ Fp = fopen ($ filename, 'w ');
Foreach ($ tables as $ table ){
Dump_table ($ table, $ fp );
}
Fclose ($ fp );
// Download the SQL file
$ File_name = $ options ['filename'];
Header ("Content-type: application/octet-stream ");
Header ("Content-Disposition: attachment; filename =". $ file_name );
Readfile ($ file_name );
// Delete the SQL file on the server
Unlink ($ file_name );
Exit;
// Obtain the table name
Function list_tables ($ database)
{
$ Rs = mysql_list_tables ($ database );
$ Tables = array ();
While ($ row = mysql_fetch_row ($ rs )){
$ Tables [] = $ row [0];
}
Mysql_free_result ($ rs );
Return $ tables;
}
// Export the database
Function dump_table ($ table, $ fp = null)
{
$ Need_close = false;
If (is_null ($ fp )){
$ Fp = fopen ($ table. '. SQL', 'w ');
$ Need_close = true;
}
$ A = mysql_query ("show create table '{$ table }"');
$ Row = mysql_fetch_assoc ($ a); fwrite ($ fp, $ row ['create Table']. ';'); // export the Table structure
$ Rs = mysql_query ("SELECT * FROM '{$ table }"');
While ($ row = mysql_fetch_row ($ rs )){
Fwrite ($ fp, get_insert_ SQL ($ table, $ row ));
}
Mysql_free_result ($ rs );
If ($ need_close ){
Fclose ($ fp );
}
}
// Export table data
Function get_insert_ SQL ($ table, $ row)
{
$ SQL = "INSERT INTO '{$ table}' VALUES (";
$ Values = array ();
Foreach ($ row as $ value ){
$ Values [] = "'". mysql_real_escape_string ($ value )."'";
}
$ SQL. = implode (',', $ values ).");";
Return $ SQL;
}?>

Export. SQL file/mysql tutorial database tutorial backup program? Php $ database = ''; // database name $ options = array ('hostname' ='', // IP address 'charset' = 'utf8 ', // Encode 'filename '...

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.