Advantages and disadvantages of mysqli_set_charset and SETNAMES in php

Source: Internet
Author: User
Advantages and disadvantages of mysqli_set_charset and SETNAMES in php

  1. // Php-5.2.11-SRC/ext/mysqli/mysqli_nonapi.c line 342
  2. PHP_FUNCTION (mysqli_set_charset)
  3. {
  4. MY_MYSQL * mysql;
  5. Zval * mysql_link;
  6. Char * cs_name = NULL;
  7. Unsigned int len;
  8. If (zend_parse_method_parameters (ZEND_NUM_ARGS () TSRMLS_CC, getThis ()
  9. , "OS", & mysql_link, mysqli_link_class_entry, & cs_name, & len) = FAILURE ){
  10. Return;
  11. }
  12. MYSQLI_FETCH_RESOURCE (mysql, MY_MYSQL *, & mysql_link, "mysqli_link"
  13. , MYSQLI_STATUS_VALID );
  14. If (mysql_set_character_set (mysql-> mysql, cs_name )){
  15. // ** Call the corresponding libmysql function
  16. RETURN_FALSE;
  17. }
  18. RETURN_TRUE;
  19. }
  20. ?>

What does mysql_set_character_set do?

  1. // Mysql-5.1.30-SRC/libmysql/client. c, line 3166:
  2. Int STDCALLmysql_set_character_set (MYSQL * mysql, const char * cs_name)
  3. {
  4. Structcharset_info_st * cs;
  5. Const char * save_csdir = charsets_dir;
  6. If (mysql-> options. charset_dir)
  7. Charsets_dir = mysql-> options. charset_dir;
  8. If (strlen (cs_name) <MY_CS_NAME_SIZE &&
  9. (Cs = get_charset_by_csname (cs_name, MY_CS_PRIMARY, MYF (0 ))))
  10. {
  11. Char buff [MY_CS_NAME_SIZE + 10];
  12. Charsets_dir = save_csdir;
  13. /* Skip execution of "set names" for pre-4.1 servers */
  14. If (mysql_get_server_version (mysql) <40100)
  15. Return 0;
  16. Sprintf (buff, "set names % s", cs_name );
  17. If (! Mysql_real_query (mysql, buff, strlen (buff )))
  18. {
  19. Mysql-> charset = cs;
  20. }
  21. }
  22. // The following content is omitted
  23. ?>

We can see that in addition to "set names", mysqli_set_charset has done one more step:

  1. Sprintf (buff, "set names % s", cs_name );
  2. If (! Mysql_real_query (mysql, buff, strlen (buff )))
  3. {
  4. Mysql-> charset = cs;
  5. }
  6. ?>

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 ):

  1. $ Db = mysql_connect ('localhost: 100', 'root', '123 ');
  2. Mysql_select_db ("test ");
  3. $ A = "\ x91 \ x5c"; // gbk encoding for "bytes". the low byte is 5c, that is, "\" in ascii "\"
  4. Var_dump (addslashes ($ ));
  5. Var_dump (mysql_real_escape_string ($ a, $ db ));
  6. Mysql_query ("set names gbk ");
  7. Var_dump (mysql_real_escape_string ($ a, $ db ));
  8. Mysql_set_charset ("gbk ");
  9. Var_dump (mysql_real_escape_string ($ a, $ db ));
  10. ?>

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

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.