This article mainly share with you the PHP backup MySQL database code example, I hope that you can master the skills in this article will use PHP backup MySQL database.
function Actionhaha () {header ("content-type:text/html; Charset=utf-8 ");//BACKUP Database $host =" localhost "; $user = "root";//Database account $password = "r123oot";//Database Password $dbname = "auth";//Database name//The account number, password, and name are all sent from the page if (!mysql_con Nect ($host, $user, $password))//connect to MySQL database {echo ' database connection failed, please check and try again '; Exit if (!mysql_select_db ($dbname))//exists the database {echo ' does not exist database: '. $dbname. ', please check and try again '; Exit } mysql_query ("Set names ' UTF8 '"); $mysql = "Set CharSet utf8;\r\n"; $q 1=mysql_query ("Show Tables"); while ($t =mysql_fetch_array ($q 1)) {$table = $t [0]; $q 2=mysql_query ("Show create Table ' $table '"); $sql =mysql_fetch_array ($q 2); $mysql. = $sql [' Create Table ']. "; r\n "; $q 3=mysql_query ("select * from ' $table '"); while ($data =mysql_fetch_assoc ($q 3)) {$keys =array_keys ($data); $keys =array_map (' addslashes ', $keys); $keys=join (', ', ', $keys); $keys = "'". $keys. " `"; $vals =array_values ($data); $vals =array_map (' addslashes ', $vals); $vals =join ("', '", $vals); $vals = "'". $vals. "'"; $mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); \ r \ n"; }} $filename = "./databak/". $dbname. Date (' Ymjgi '). ". SQL "; Storage path, default to the outermost layer of the project $fp = fopen ($filename, ' w '); Fputs ($fp, $mysql); Fclose ($FP); echo "Data backup succeeded"; exit; }