Program BACKUP Database

Source: Internet
Author: User

Tag:io   ar   os   sp   for    file    on    data    bs   

<?phpheader ("Content-type:text/html;charset=utf-8"); $database = "Thinksns_3_1"; $link = mysql_connect (' LocalHost ', ' root ', ' 123456 ') or Die (' Connection failed '), mysql_select_db ($database) or Die (' connection database failed ');//Select Encoding mysql_query ("set Names UTF8 ")//1. Get all Tables $rs = mysql_query (" SHOW TABLES from $database "), $tableArray = Array (); while ($row = Mysql_fetch_ro W ($rs)) {$tableArray [] = $row [0];} Mysql_free_result ($rs);//2. Profile Name $to_file_name = "./". Time (). SQL "; $info ="/* ". Date (' y-m-d ')." Export Data */\r\n "; File_put_contents ($to _file_name, $info, file_append);//3. Loop table foreach ($tableArray as $val) {$sql =" show CREATE TABLE ". $val; $res = mysql_query ($sql, $link); $row = Mysql_fetch_array ($res); $info ="/*--table structure for ". $va L. " ' */\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 Resources mysql_free_result ($res); $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 = "/*--Records for". $val. " ' */\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);} Header ("Content-type:application/octet-stream"), Header ("Accept-ranges:bytes"), Header ("Accept-length:". FileSize ($to _file_name)); Header ("content-disposition:attachment; Filename= ". $to _file_name); ReadFile ($to _file_name); echo "Data all Around";

Program BACKUP Database

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.