- // Php-5.2.11-SRC/ext/mysqli/mysqli_nonapi.c line 342
- PHP_FUNCTION (mysqli_set_charset)
- {
- MY_MYSQL * mysql;
- Zval * mysql_link;
- Char * cs_name = NULL;
- Unsigned int len;
-
- If (zend_parse_method_parameters (ZEND_NUM_ARGS () TSRMLS_CC, getThis ()
- , "OS", & mysql_link, mysqli_link_class_entry, & cs_name, & len) = FAILURE ){
- Return;
- }
- MYSQLI_FETCH_RESOURCE (mysql, MY_MYSQL *, & mysql_link, "mysqli_link"
- , MYSQLI_STATUS_VALID );
-
- If (mysql_set_character_set (mysql-> mysql, cs_name )){
- // ** Call the corresponding libmysql function
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- ?>
What does mysql_set_character_set do?
- // 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 content is omitted
- ?>
-
We can see that in addition to "set names", mysqli_set_charset has done one more step:
- Sprintf (buff, "set names % s", cs_name );
- If (! Mysql_real_query (mysql, buff, strlen (buff )))
- {
- Mysql-> charset = cs;
- }
- ?>
-
What is the role of charset, a member of the mysql core structure? This tells us about mysql_real_escape_string (). The difference between this function and mysql_escape_string is that it considers the "current" character set. Where can this current character set come from? By the way, you guessed it was mysql-> charset. Mysql_real_string in the determination of wide character set characters, according to the member variables to adopt different policies, for example, if it is UTF-8, then libmysql/ctype-utf8.c will be used. Check the instance. the default mysql connection character set is latin-1 (classic 5c problem ):
- $ Db = mysql_connect ('localhost: 100', 'root', '123 ');
- Mysql_select_db ("test ");
- $ A = "\ x91 \ x5c"; // gbk encoding for "bytes". the low byte is 5c, that is, "\" in ascii "\"
-
- Var_dump (addslashes ($ ));
- 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 ));
- ?>
Because the gbk encoding of "bytes" is low-byte 5c, that is, "\" in ascii, and because mysql (I) _ set_charset affects mysql-> charset, the default value is mysql> charset at other times. Therefore, the result is $ php-f 5c. phpstring (3) "inline \" string (3) "inline \" string (3) "inline \" string (2) "inline" This article |