Why are the exported data in Chinese garbled characters and the following code is used to back up the database? why are the exported data in Chinese garbled characters ??
// Back up the database
Define ('in _ fdyu', true );
Require ('Des/config. php ');
// The account, password, and name are all uploaded from the page.
If (! Mysql_connect ($ db_host, $ db_user, $ db_pass) // connect to the mysql database
{
Echo 'database connection failed. check the database and try again ';
Exit;
}
If (! Mysql_select_db ($ db_name) // check whether the database exists
{
Echo 'database does not exist: '. $ db_name.'. check the database and try again ';
Exit;
}
Mysql_query ("set names 'utf8 '");
$ Mysql = "set charset utf8; \ r \ n ";
$ Q1 = mysql_query ("show tables ");
While ($ t = mysql_fetch_array ($ q1 ))
{
$ Table = $ t [0];
$ Q2 = mysql_query ("show create table '$ table "');
$ SQL = mysql_fetch_array ($ q2 );
$ Mysql. = $ SQL ['create Table']. "; \ r \ n ";
$ Q3 = mysql_query ("select * from '$ table "');
While ($ data = mysql_fetch_assoc ($ q3 ))
{
$ 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, which is stored in the outermost layer of the project by default
$ 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 windows.
When the content of a text file is UTF-8 encoded and does not have a BOM header, it is normal to use the editor to open the file with garbled characters.
What should I do?
Wrong. it is normal to import the exported files to the database, but when I import the exported files to the database, the Chinese characters in the data are garbled.
The character set must be declared when imported.
Set names utf8
Set names utf8
$ Mysql = "set charset utf8; \ r \ n"; change this to $ mysql = "set names utf8; \ r \ n"; that's all. thank you!