Here is the code to back up the database, but why the data is garbled in Chinese??
Backing Up the database
Define (' In_fdyu ', true);
Require (' includes/config.php ');
The account number, password, and name are all transmitted from the page.
if (!mysql_connect ($db _host, $db _user, $db _pass))//Connect MySQL Database
{
Echo ' Database connection failed, please check and try again ';
Exit
}
if (!mysql_select_db ($db _name))//whether the database exists
{
Echo ' does not exist database: '. $db _name. ', 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 = $db _name. "_" . Date (' y-m-d ', Time ()). ". SQL"; Storage path, default to the outermost layer of the project
$fp = fopen ($filename, ' w ');
Fputs ($fp, $mysql);
Fclose ($FP);
Header (' location:index.php ');
?>
Reply to discussion (solution)
$mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); \ r \ n ";
It should be in the Windows environment.
When the text file content is UTF-8 encoded and there is no BOM header, open with the editor is garbled, this is normal
How do you get that?
Wrong, the export is normal, but I will import the file into the database, the data in the Chinese is garbled
In the back, you have to declare the character set.
Set Names UTF8
Set Names UTF8
$mysql = "Set CharSet utf8;\r\n"; change this to $mysql = "Set names utf8;\r\n"; Yes, thank you!