I most often use set names to solve the PHP and MySQL garbled problem
Program settings
The code is as follows |
Copy Code |
mysql_query ("SET NAMES UTF8"); My.ini settings # CLIENT Section [MySQL] Default-character-set=utf8 # SERVER Section [Mysqld] Default-character-set=utf8
|
These two fields to change the default character set for the database. The first is the client default character set, and the second is the server-side default character set. Suppose we set all two to UTF8 and then enter "show Variebles like" character_set_% in the MySQL Command line client and see the following characters:
The code is as follows |
Copy Code |
Character_set_client latin1 Character_set_connection latin1 Character_set_database UTF8 Character_set_results latin1 Character_set_server UTF8 Character_set_system UTF8
|
The UTF8 changes as we set above. At this point, if we read data from a database using a UTF-8 PHP program, it's probably a bunch of "?????" Or else it's garbled. Online search for a half-day, the solution is simple, after connecting the database, read the data before the first execution of a query "SET NAMES UTF8", that is, in PHP for
You can display normal (as long as the character of the information in the database is normal). Why is that? This query "SET NAMES UTF8" in the end what is the role?
To the MySQL command line, enter "SET NAMES UTF8;" And then execute "show variebles like" character_set_% ", and find those variables" Latin1 "that were originally character_set_client," Character_set_connection "," Character_set_results "the value of all into UTF8, the original is the 3 variables in mischief. Consult the manual, the above sentence is equal to:
The code is as follows |
Copy Code |
SET character_set_client = UTF8;
SET character_set_results = UTF8; SET character_set_connection = UTF8; |
Here to declare that the "SET NAMES UTF8" function is only temporary, MySQL restart after the default.
Next, we'll talk about MySQL's configuration on the server. Do we have to add "SET NAMESUTF8" to the database every time we read and write to ensure that the encoding of the data is consistent? Can you configure MySQL to reach those three variables by default for the character set we want to think about? Not on the manual, I didn't find the answer on the Internet either. So, from the server configuration point of view, there is no way to omit that line of code
And what did Mysql_set_character_set do?
The code is as follows |
Copy Code |
MYSQL-5.1.30-SRC/LIBMYSQL/CLIENT.C, line 3166: int Stdcallmysql_set_character_set (mysql*mysql, const char *cs_name) { Structcharset_info_st *cs; const char *save_csdir= charsets_dir;
if (Mysql->options.charset_dir) Charsets_dir= mysql->options.charset_dir;
if (strlen (cs_name) < my_cs_name_size && (cs= get_charset_by_csname (Cs_name, My_cs_primary, MYF (0))) { Char buff[my_cs_name_size + 10]; Charsets_dir= Save_csdir; /* Skip Execution of "SET NAMES" for pre-4.1 servers * * if (mysql_get_server_version (MySQL) < 40100) return 0; sprintf (Buff, "SET NAMES%s", cs_name); if (!mysql_real_query (MySQL, buff, strlen (buff)) { Mysql->charset= CS; } } The following omitted |
As we can see, mysqli_set_charset, in addition to doing the "set NAMES", has one more step:
The code is as follows |
Copy Code |
sprintf (Buff, "SET NAMES%s", cs_name); if (!mysql_real_query (MySQL, buff, strlen (buff)) { Mysql->charset= CS; } |
And what is the role of CharSet, a member of the core structure of MySQL?
This is going to say mysql_real_escape_string (), and the difference between this function and mysql_escape_string is that it takes into account the "current" character set. So where does this current character set come from?
By the way, you guessed right, that's mysql->charset.
Mysql_real_string in judging the character of wide character set, according to this member variable to use different strategies, such as if it is utf-8, then will use LIBMYSQL/CTYPE-UTF8.C.
code is as follows |
copy code |
<?php $db = mysql_connect (' localhost : 3737 ', ' root ', ' 123456 '); mysql_select_db ("test"); $a = "x91x5c";/"?" GBK encoding, low byte 5c, or var_dump (addslashes ($a) in ASCII; Var_dump (mysql_real_escape_string ($a, $db)); mysql_query ("Set names GBK"); Var_dump (mysql_ Real_escape_string ($a, $db)); mysql_set_charset ("GBK"); Var_dump (mysql_real _escape_string ($a, $db)); ?> |