Why can't I retrieve Chinese characters on the PHP programming webpage? However, the phpadmin database displays Chinese characters.

Source: Internet
Author: User
PHP Programming Web pages cannot obtain Chinese characters ???, However, the phpadmin database displays Chinese Characters & lt ;? Php $ conn & nbsp; = & nbsp; mysql_connect ("localhost", "root", "1234") & nbsp; or & nbsp; die ("cannot connect to the database server: ". the mysql_conne PHP programming webpage cannot obtain Chinese characters. all Chinese characters are ???, However, the phpadmin database displays Chinese characters.
$ Conn = mysql_connect ("localhost", "root", "1234") or die ("cannot connect to the database server:". mysql_connect_errror ());
Mysql_select_db ("messtest", $ conn );
$ Results = mysql_query ("select * from gbbook ");
Mysql_query ($ results, $ conn );

Mysql_query ("setnames 'utf8'", $ conn); // use Chinese encoding;
While ($ row = mysql_fetch_row ($ results )){
Echo $ row [0]. "". $ row [1]. "". $ row [2]. "". $ row [3]. "". $ row [4];
Echo" ";
}
Mysql_close ($ conn );
?>

------ Solution --------------------
Mysql_query ("set names 'utf8'", $ conn); // use Chinese encoding;
------ Solution --------------------
Combine the browser and PHP files with gbk encoding, and add the following to the first line:
Header ("content-type: text/html; charset = gbk ")
------ Solution --------------------
You can see what encoding format is during your mysql installation (in the MySQL installation directory) my. find "default-character-set" in ini (there are two parts). if latin is changed to gbk or UTF-8, the supported Chinese encoding format can be gbk UTF-8.
// Add the following code if php does not include html code.
Header ("content-type: text/html; charset = gb2312") or header ("content-type: text/html; charset = gbk ")

// If both php and html code are included, add the following code.

------ Solution --------------------
It is best to confirm your database encoding and page display encoding. The encoding must be consistent.
------ Solution --------------------
Hi, I know what went wrong with you. This is your problem, but I have fixed it.

You have such a section: mysql_query ("setnames 'utf8'", $ conn); // uses Chinese Encoding. not only is it wrong, but you only set setnames to utf8 encoding, instead of the entire database. You should delete and add mysql_query ("set names utf8"); under mysql_query ($ results, $ conn.

I'm a newbie, and so are you.

I set the webpage to utf8, but my database field is set to gbk. There is no error, and the hacker can be read.


------ Solution --------------------
The page code should also be changed
Add
Header ("content-type: text/html; charset = utf-8 ");
Add database
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.