; Then those are sent as if they were latin1 to the server (mysqld). mysql set names 问题 mysql_query("set names utf8");一直以来总以为set names 是用来设置msyql 的字符集的,最近作个东西才发现自己认识上的错误,查一下手册SET NAMES ‘x‘语句与这三个语句等价:mysql> SET character_set_client = x;mysql> SET mysql_query("set names 'utf8'"); Note that you should write the encode which you are using in the header for example if you are using utf-8 you add it like this in the header or it will couse a problem with Internet Explorer
ref: 谈谈MySQL乱码和set names 我个人觉得,由于本地机为gbk编码,所以,即使创建数据库时的编码是utf8,当我们录入数据时,仍然需要告诉mysql,本地机器编码为gbk. I think this behavior and workaround should be documented somewhere in the manual. what entries should I add there?
mysql> set names utf32; Query OK, 0 rows affected (0.00 sec) mysql> select 5; ERROR 2013 (HY000): Lost connection to MySQL server during query mysql> [6 Dec 2007 7:48] Alexander Barkov . set the 'names' (set names *) or _ANY_ 'character set' (set character set *) (opposed to what they tell you on these pages). And use SET NAMES or mysql(i)_set_charset() in PHP or something similar after each connect to a MySQL server. Note: . 1年くらい前に社内MLに投げた、「MySQLでSET NAMESを使ってはいけない理由」をコピペしてみます。手抜きです、はい。 赤字は注釈です。 今更ながら、「MySQLで SET NAMES を使ってはいけない」の根拠のお話です。 下記のPHPスクリプトでは、入力値を元にSQL文を生成し、検索クエリを投げて … The set_charset() / mysqli_set_charset() function specifies the default character set to be used when sending data to and from the database server. ; The Server says "Oh, I am getting some latin1 bytes, and I will be putting them into a latin1 column, so I don't need to transform them. mysql> set names utf32; Query OK, 0 rows affected (0.00 sec) mysql> select 5; ERROR 2013 (HY000): Lost connection to MySQL server during query mysql> [6 Dec 2007 7:48] Alexander Barkov . So the values sent from the client program are stored 'as is' in the table because the client character set and the table's character set is the same. 6. This is the preferred way to change the charset. 7. That’s all you need. Note: . Patricio wrote on 31st July 2012 at 02:36: Nice article Mathias, this also took me by surprise some time ago.
mysql_query( ) で (SET NAMES utf8 などとして) 設定する方法はお勧めできません。 詳細は MySQL の文字セットの概念 を参照ください。 参考 How to make PDO adapter run SET NAMES utf8 each time I connect, In ZendFramework. The PHP manual explicitly warns us to use mysqli_set_charset , not SET NAMES : This is the preferred way to change the charset.
If it wasn't clear, I am looking for the correct syntax to do it in the config.ini file of my project and not in php code, as I regard this part of the configuration code. By executing "set names utf8", it sets the client character set variables to utf8. This function requires MySQL 5.0.7 or later. I am using an INI file to save the adapter config data. Notes. Check the previous item by listing the results of the mysql query 'SHOW session VARIABLES'. Note: For this function to work on a Windows platform, you need MySQL client library 4.1.11 or above (for MySQL 5.0, you need 5.0.6 or above).