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

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

我正在从php mysql_ *方法将网站迁移到php mysqli中。

我有以下代码可以完成这项工作:

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

没有此查询,我的字符串字符(使用格鲁吉亚语)带有问号。
例如,它写成????????? 代替 ?????????

因此,由于它能完成工作,所以我很高兴,但是现在我无法对mysqli进行同样的操作。

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

有人可以帮我吗? 谢谢。


不建议使用mysqli查询来设置名称,而是使用mysqli :: set_charset

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


PHP功能请求/错误报告已提交...

请参阅https://bugs.php.net/bug.php?id=52267,该文档从[email protected]获得了响应:

...use mysqli_set_charset() to set the charset and then SET NAMES to change the collation.

他/她还链接到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. 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()

并且我将链接到http://dev.mysql.com/doc/refman/5.6/en/charset-collate.html,其中显示了如何使用适合该查询的排序规则进行查询。

With the COLLATE clause, you can override whatever the default collation is for a comparison. COLLATE may be used in various parts of SQL statements. Here are some examples:

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

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

  • With 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:
    SELECT DISTINCT k COLLATE latin1_german2_ci
    FROM t1;

  • With 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:
    SELECT k
    FROM t1
    GROUP BY k
    HAVING k = _latin1 'Müller' COLLATE latin1_german2_ci;


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

要么

mysqli->set_charset("utf8")


您可以使用mysqli_set_charset

This is the preferred way to change the charset. Using mysqli_query()
to set it (such as SET NAMES utf8) is not recommended.

但是,要设置排序规则,您仍然必须使用SET NAMES查询。


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