php PDO + MySQL 和损坏的 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4475548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO + MySQL and broken UTF-8 encoding
提问by Jason4Ever
I use the PDO library with a MySQL database in PHP, but if I insert any data encoded in UTF-8, like Arabic words, it's inserted into the database, but as ?????????
.
我将 PDO 库与 PHP 中的 MySQL 数据库一起使用,但是如果我插入任何以 UTF-8 编码的数据,如阿拉伯语单词,它会被插入到数据库中,但作为?????????
.
In my own framework, after I create the PDO connection, I send two queries – SET NAMES utf8
and SET CHARACTER SET utf8
. It still doesn't work.
在我自己的框架中,在创建 PDO 连接后,我发送了两个查询 -SET NAMES utf8
和SET CHARACTER SET utf8
. 它仍然不起作用。
Example:
例子:
loadclass('PDO', array(
sprintf(
'mysql:host=%s;port=%s;dbname=%s',
confitem('database', 'host'),
confitem('database', 'port'),
confitem('database', 'name')
),
confitem('database', 'username'),
confitem('database', 'password'),
array('PDO::ATTR_PERSISTENT' => confitem('database', 'pconnect'))
));
$this->query('SET NAMES ' . confitem('database', 'charset'));
$this->query('SET CHARACTER SET ' . confitem('database', 'charset'));
Workaround: Use the json_encode
function to convert data before inserting it to the database, and use json_decode
to decode it after fetching. This is how I do it now.
解决方法:在json_encode
将数据插入数据库之前使用该函数进行数据转换,并json_decode
在获取后使用该函数对其进行解码。这就是我现在的做法。
回答by shark555
Use:
用:
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
It forces UTF-8 on the PDO connection. It worked for me.
它在 PDO 连接上强制使用 UTF-8。它对我有用。
回答by Palec
You have to set the correct character set for the connection. Add the charset=utf8
option to the DSN(this is MySQL-specific!)
您必须为连接设置正确的字符集。将charset=utf8
选项添加到 DSN(这是 MySQL 特定的!)
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName;charset=utf8',
'username',
'password'
);
However, in PHP versions before 5.3.6, you must use a workaround as the charset
option is not supported.
但是,在 5.3.6 之前的 PHP 版本中,您必须使用变通方法,因为该charset
选项不受支持。
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
回答by bbe
All attempts like:
所有的尝试都像:
PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' "
or
或者
$this->connection = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8', DBUSER, DBPASS, self::$opt);
or
或者
$this->connection->exec("set names utf8");
still generated unreadable text mess.
仍然产生不可读的文本混乱。
In my case, the cause of the problem was: htmlentities
used prior to inserting data into a database.
Cyrillic letterswere destroyed completely.
就我而言,问题的原因是:htmlentities
在将数据插入数据库之前使用。
西里尔字母被完全摧毁。
回答by Burak Guzel
Try setting the default_charset
value in php.ini to UTF-8. Or you can set it using the ini_setfunction.
尝试将default_charset
php.ini 中的值设置为 UTF-8。或者您可以使用ini_set函数设置它。
Also, if the input is coming through form submissions, make sure your web pages are set to UTF-8 using the meta tag.
此外,如果输入来自表单提交,请确保您的网页使用元标记设置为 UTF-8。