[英]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")
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 thenSET 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 ofmysql->charset
, and thus affects the character set used bymysql_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-collate.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.