简体   繁体   English

php mysql SET NAMES 'utf8' COLLATE 'utf8_unicode_ci' 不适用于 mysqli

[英]php mysql SET NAMES 'utf8' COLLATE 'utf8_unicode_ci' doesn't work with mysqli

I am migrating my site into php mysqli from php mysql_* methods.我正在将我的网站从 php mysql_* 方法迁移到 php mysqli。

I had following code that did the job:我有以下代码来完成这项工作:

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'"); mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");

Without this query my string characters (in Georgian language) were written with question marks.如果没有这个查询,我的字符串字符(格鲁吉亚语)是用问号写的。 For example it was written ?????????例如它被写成????????? instead of გამარჯობა而不是გამარჯობა

So since it did its job I was happy, but now I cannot do the same with mysqli.所以既然它完成了它的工作,我很高兴,但现在我不能用 mysqli 做同样的事情。

$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");

Can anyone please help me out?任何人都可以帮我吗? Thanks.谢谢。

It is not recommended to use mysqli query in order to set names but rather mysqli::set_charset 建议不要使用mysqli查询来设置名称,而是使用mysqli :: set_charset

$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->set_charset("utf8");

You can use mysqli_set_charset 你可以使用mysqli_set_charset

This is the preferred way to change the charset. 这是更改charset的首选方法。 Using mysqli_query() to set it (such as SET NAMES utf8) is not recommended. 建议不要使用mysqli_query()来设置它(例如SET NAMES utf8)。

However, to set collation, you will still have to use the SET NAMES query. 但是,要设置排序规则,您仍然必须使用SET NAMES查询。

http://php.net/manual/en/mysqli.set-charset.php

要么

mysqli->set_charset("utf8")

A PHP feature request/bug report was filed... 提交了PHP功能请求/错误报告......

See https://bugs.php.net/bug.php?id=52267 which garnered the response from uw@php.net: 请参阅https://bugs.php.net/bug.php?id=52267 ,其中收到了来自uw@php.net的回复:

...use mysqli_set_charset() to set the charset and then SET NAMES to change the collation. ...使用mysqli_set_charset()设置charset,然后使用SET NAMES更改排序规则。

He/she also links to http://dev.mysql.com/doc/refman/5.1/en/mysql-set-character-set.html 他/她还链接到http://dev.mysql.com/doc/refman/5.1/en/mysql-set-character-set.html

This function is used to set the default character set for the current connection. 此函数用于设置当前连接的默认字符集。 The string csname specifies a valid character set name. 字符串csname指定有效的字符集名称。 The connection collation becomes the default collation of the character set. 连接排序规则成为字符集的默认排序规则。 This function works like the SET NAMES statement, but also sets the value of mysql->charset , and thus affects the character set used by mysql_real_escape_string() 此函数与SET NAMES语句类似,但也设置mysql->charset的值,从而影响mysql_real_escape_string()使用的字符集

And I'll link to http://dev.mysql.com/doc/refman/5.6/en/charset-collate.html which shows how to make queries using whatever collation suits that query. 我将链接到http://dev.mysql.com/doc/refman/5.6/en/charset-collat​​e.html ,其中显示了如何使用查询的任何排序规则进行查询。

With the COLLATE clause, you can override whatever the default collation is for a comparison. 使用COLLATE子句,您可以覆盖默认排序规则以进行比较。 COLLATE may be used in various parts of SQL statements. COLLATE可用于SQL语句的各个部分。 Here are some examples: 这里有些例子:

  • With ORDER BY : 使用ORDER BY
    SELECT k FROM t1 ORDER BY k COLLATE latin1_german2_ci;

  • With AS : 使用AS
    SELECT k COLLATE latin1_german2_ci AS k1 FROM t1 ORDER BY k1;

  • With GROUP BY : 使用GROUP BY
    SELECT k FROM t1 GROUP BY k COLLATE latin1_german2_ci;

  • With aggregate functions: 使用聚合函数:
    SELECT MAX(k COLLATE latin1_german2_ci) FROM t1;

  • With DISTINCT : 使用DISTINCT
    SELECT DISTINCT k COLLATE latin1_german2_ci FROM t1;

  • With WHERE : 使用WHERE
    SELECT * FROM t1 WHERE _latin1 'Müller' COLLATE latin1_german2_ci = k; SELECT * FROM t1 WHERE k LIKE _latin1 'Müller' COLLATE latin1_german2_ci;

  • With HAVING : 随着HAVING
    SELECT k FROM t1 GROUP BY k HAVING k = _latin1 'Müller' COLLATE latin1_german2_ci;

$con=mysqli_connect("localhost","admin","1234","web");
$con->set_charset("utf8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PHP mysql_ * SET NAMES UTF 8和Mysql Table和utf8_unicode_ci进行字符串比较 - String Comparison using PHP mysql_* SET NAMES UTF 8 and Mysql Table With utf8_unicode_ci utf8_unicode_ci似乎不是UTF8 - utf8_unicode_ci seems not to be UTF8 使用“utf8_unicode_ci”在 PHP 中创建 MySQL 表 - CREATE MySQL TABLE in PHP with “utf8_unicode_ci” PHP 读取以 utf8_unicode_ci 编码的 mysql 数据 - PHP reading a mysql data encoded in utf8_unicode_ci 列上的mysql最大长度并确保我没有使用utf8_unicode_ci - PHP来超过这个限制? - mysql max length on columns and ensuring I don't go over that limit using utf8_unicode_ci - PHP? Mysql字符集从utf8_unicode_ci解码以转换jis - Mysql character set decoding from utf8_unicode_ci to shift jis MySql / php utf8似乎无法正常工作 - MySql/php utf8 doesn't seem to work right 日语/英语输入的mysql数据库:utf8_unicode_ci或utf8mb4_unicode_ci? - mysql database for Japanese/English input: utf8_unicode_ci or utf8mb4_unicode_ci? 使用JSON PHP脚本在Android应用中的MySQL服务器中显示保存在varchar(utf8_unicode_ci)中的语言文本 - Show lingual text kept in varchar (utf8_unicode_ci) in MySQL server in Android app with JSON php script utf8_unicode_ci的JSON数据的PHP标头中的内容类型字符集-Ajax调用 - Content Type Character Set in PHP Header of JSON Data for utf8_unicode_ci - Ajax Call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM