Mysql Chinese language cannot be read.

Source: Internet
Author: User
Mysql Chinese cannot be read... This is the encoding method of the mysql database. this is the result of background data. if you do not use Chinese, the data is normally written to the background. if you use Chinese, the data cannot be written. (Pay attention to the last few rows of data) if you write Chinese characters directly to the database, it will be displayed. PHP code & lt ;? Phpif (isset ($ _ POST ['b1 ']) mysql cannot read Chinese characters...

This is the encoding method of the mysql database.

This is the result of background data. if you do not use Chinese, the data is normally written to the background. if you use Chinese, the data cannot be written. (Check the last few rows of data)
If you write Chinese characters directly to the database, it will be displayed.
PHP code
If (isset ($ _ POST ['b1 '])
{
$ Username = $ _ POST ["username"];
$ Message = $ _ POST ["message"];
If ($ username = "")
{
Echo" Alert ('Enter your name! '); Script ";
If ($ message = "")
{
Echo" Alert ('Enter the message content! '); Script ";
}
}
Else
{
$ Addtime = date ("y-m-d h: m: s ");
$ Id = mysql_connect ("localhost", "root", "1234 ");
Mysql_select_db ("gbook", $ id );
$ Query = "insert into message (author, addtime, content, reply) values ('$ username',' $ addtime', '$ message ','')";
$ Result = mysql_query ($ query, $ id );
Mysql_close ($ id );
// Echo "result =". $ result;
Echo" Alert ('message successful, click OK to view the message! '); Location. href = 'index. php'; script ";
}
}
------ Solution --------------------

$username=iconv('gb2312','utf-8',$_POST["username"]);
$message=iconv('gb2312','utf-8',$_POST["message"]);

------ Solution --------------------
The database code is utf8.
If you write Chinese characters directly to the database, it will be displayed. (The last line)
Phpmyadmin will perform encoding settings, so there is no problem naturally.
Your code is not encoded, so it is normal to have a problem!
When the file encoding is gbk (ANSI in windows), it should
Mysql_query ('set names gbk ');
When the file encoding is UTF-8, it should be
Mysql_query ('set names utf8 ');


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.