“set names” vs mysqli_set_charset — besides affecting mysqli_escape_string, are they identical?

前端 未结 4 590
遥遥无期
遥遥无期 2021-01-04 09:53

It seems to be common knowledge to use mysql_set_charset / mysqli::set_charset instead of the direct MySQL query set names.

The reason often cited is that se

4条回答
  •  猫巷女王i
    2021-01-04 10:11

    SET NAMES ... is a convenience alias:

    A SET NAMES 'charset_name' statement is equivalent to these three statements:

    SET character_set_client = charset_name;
    SET character_set_results = charset_name;
    SET character_set_connection = charset_name;
    

    Setting character_set_connection to charset_name also implicitly sets collation_connection to the default collation for charset_name.

    ... that provides MySQL Server with all the text-encoding information required for current connection. So far so good.

    But PHP is also involved and it will not learn anything from here because it's basically a random user query. There are two things that PHP will not do for obvious performance reasons:

    • Scan all user queries sent to the server to detect calls to SET NAMES.
    • Ask MySQL for current values of the involved directives every time something needs to be done.

    In short: this method notifies the server but not the client. However, dedicated PHP functions do both things.

提交回复
热议问题