Today encountered a database library is UTF8, table is also UTF8, but the content is GBK problem, appeared in Chinese garbled
The first step is to copy a copy of all the tables, such as the table named User,copy a light table structure , called user2;
The second step is to transcode the data in table 2 into the table
<?phpheader ("content-type:text/html; Charset=utf-8 "); function Get_sql ($tablename) {$con = mysql_connect ("localhost", "root", "root"), if (! $con) Die (Mysql_error ()); Mysql_ select_db ("Bfwl", $con); mysql_query ("Set names ' Utf-8 '"); $result = mysql_query ("SELECT * from". $tablename. " $txt = "", while ($row = Mysql_fetch_array ($result)) {$sql = ""; foreach ($row as $k + = $v) {if (!is_numeric ($k)) {$v = Iconv ("GBK", "UTF-8", $v), $v = Str_replace ("\ r \ n", "<br>", $v), $v = Trim ($v), $v = Str_replace ("'", "" ", $v); $v = str _replace (' "'," "" ", $v); $sql. =" $k = ' $v ', ";}} $sql = Trim ($sql, ","), $sql = "INSERT into". $tablename. " 2 SET ". $sql."; \ r\n "; Excute ($sql); $txt. = $sql;} File_put_contents ("sql/". $tablename. ". SQL ", $txt);} function Excute ($sql) {$con = mysql_connect ("localhost", "root", "root"), if (! $con) Die (Mysql_error ()); mysql_select_db ("Bfwl", $con); mysql_query ("Set names UTF8"); mysql_query ($sql); Mysql_close ($con);} Each table comes once Get_sql ("Bingzhongbiao"), Get_sql ("Chat_record"), Get_sql ("Juesebiao");Get_sql ("Meitibiao"), Get_sql ("Net_chz"), Get_sql ("Net_xf_biao"), Get_sql ("Yishengbiao"), Get_sql ("Yiyuanbiao"); Get_sql ("Yonghubiao"); Get_sql ("Zaixian"); Get_sql ("Zhanzhang");
Step three, delete the original table, rename the 2 table OK
MySQL table UTF8 encoding, content GBK encoded garbled conversion