MySQL table UTF8 encoding, content GBK encoded garbled conversion

Source: Internet
Author: User

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

Related Article

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.