php Chinese garbled problems and MySql Chinese garbled solution

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags display html mysql mysql tutorial names network network programming php

Chinese garbled Chinese tutorial and mysql tutorial Chinese garbled solution

<html>
<head>
<title> </ title>
</ head>
<body>
<? php
$ mysql_server_name = 'localhost';
$ mysql_username = 'root';
$ mysql_password = '000000';
$ mysql_database = 'lib';
$ conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password, $ mysql_database);
$ sql = "select name, age from mytb";
print ($ conn);
$ rs = mysql_db_query ("lib", "select * from mytb", $ conn);
print ("
<br> ");
while ($ row = mysql_fetch_object ($ rs)) {
print ($ row-> name. ":". $ row-> age. "<br>");
}
mysql_close ($ conn);
?>

Display as follows:

resource id # 1

dd: 54
ddd: 8
??: 15
???: 25
??: 32


Mysql encoding: utf8, gbk have tried. mysql font and command line display are correct.

Questions added:

Garbled

???: 15
???: 25
??: 32


These few lines, the value of the database tutorial is Chinese characters. The question mark is displayed.

Solution:

In $ rs = mysql_db_query ("lib", "select * from mytb", $ conn);

Plus in front

mysql_query ("set names gb2312"); or mysql_query ("set names gbk");


Now look at the solution to the problem of garbled


.gb2312, gbk, utf8 and other support multi-byte encoding character sets can store Chinese characters, the number of characters in gb2312 much less than gbk, and gb2312, gbk, etc. can be encoded under utf8.

Use the command show variables like 'character_set_%'; View the current character set settings:

mysql> show variables like 'character_set_%';
+ -------------------------- + -------- +
| variable_name | value |
+ -------------------------- + -------- +
character_set_client | gb2312 |
character_set_connection | gb2312 |
character_set_database | gb2312 |
character_set_filesystem | binary |
character_set_results | gb2312 |
character_set_server | latin1 |
character_set_system | utf8 |
+ -------------------------- + -------- +
7 rows in set (0.02 sec)

(The character set here is gb2312)

Display Chinese garbled There are two main settings: character_set_connection and character_set_results,

If your two settings do not support Chinese encoding, there will be garbled, as long as: set character_set_results = gbk; Set the Chinese encoding.

Three .set names charset_name; You can set all the client's character set.

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.