In-depth understanding of the differences between set names and mysql (I) _ set_charset

Source: Internet
Author: User

Recently, the company organized a PHP security programming training, which involves some of the content about Mysql's "set names" and mysql_set_charset (mysqli_set_charset): Speaking of, try to use mysqli_set_charset (mysqli: set_charset) instead of "set names", of course, this content is also described in the PHP manual, but it does not explain why. recently, several friends asked me this question. Why? When there are too many people to ask, I think I can write a blog to introduce this part of content. first, many people do not know what "set names" is. My previous articles have gone deep into MySQL Character SET settings, I have introduced character_set_client/character_set_connection/character_set_results, the three MySQL "environment variables". Here I will briefly introduce these three variables, which respectively tell the MySQL server and the consumer set of the client, the replica set that is transmitted to the MySQL server and the replica set that expects the results returned by MySQL. for example, by using "set names utf8", I will tell the server that I am using UTF-8 encoding. I hope you will also return the UTF-8 encoding query results to me. generally, it is enough to use "set names" to ensure correctness. so why is mysqli_set_charset (PH) recommended in the manual? P> = 5.0.5? First, let's take a look at what mysqli_set_charset has done (note the asterisk comment, mysql_set_charset is similar): // php-5.2.11-SRC/ext/mysqli/line 342PHP_FUNCTION (mysqli_set_charset) {MY_MYSQL * mysql; zval * mysql_link; char * cs_name = NULL; unsigned int len; if (convert (ZEND_NUM_ARGS () TSRMLS_CC, getThis (), "OS", & mysql_link, mysqli_link_class_entry, & cs_name, & len) = FAILURE) {return;} MYSQLI_FETCH_RESOUR CE (mysql, MY_MYSQL *, & mysql_link, "mysqli_link", MYSQLI_STATUS_VALID); if (mysql_set_character_set (mysql-> mysql, cs_name )) {// ** call the corresponding function RETURN_FALSE of libmysql;} RETURN_TRUE;} What does mysql_set_character_set do? /// Mysql-5.1.30-SRC/libmysql/client. c, line 3166: int STDCALL mysql_set_character_set (MYSQL * mysql, const char * cs_name) {struct charset_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 = Hangzhou (cs_name, MY_CS_PRIMARY, MYF (0) {char buff [MY_CS_NAME_SIZE + 10]; cha Rsets_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 ;}// we can see that mysqli_set_charset has not only set names, make another 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): <? Php $ db = mysql_connect ('localhost: 808080', 'root', '000000'); mysql_select_db ("test"); $ a = "\ x91 \ x5c "; // gbk encoding of "bytes". The low byte is 5c, that is, "\" var_dump (addslashes ($ a); var_dump (mysql_real_escape_string ($, $ db); mysql_query ("set names gbk"); var_dump (mysql_real_escape_string ($ a, $ db); mysql_set_charset ("gbk"); var_dump (cost ($, $ 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) "character \" string (3) "character \" string (3) "character \" string (2) "character" is clear now?

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.