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

2021-12-25 00:00:00 php mysqli

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

I am migrating my site into php mysqli from php mysql_* methods.

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

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 გამარჯობა

所以既然它完成了它的工作,我很高兴,但现在我不能用 mysqli 做同样的事情.

So since it did its job I was happy, but now I cannot do the same with mysqli.

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

谁能帮帮我?谢谢.

推荐答案

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

It is not recommended to use mysqli query in order to set names but rather mysqli::set_charset

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

相关文章