mysql 特殊字符截断,PHP / Mysql特殊字符插入被截断

在使用PDO与MySQL5.1.41交互时,遇到插入包含特殊字符(如é)的单词被截断的问题。设置PDO的MYSQL_ATTR_INIT_COMMAND属性为SET NAMES 'utf8'并未解决问题。解决方案是先使用utf8_encode将特殊字符转码,然后再执行插入操作,成功解决了字符截断问题。
摘要由CSDN通过智能技术生成

I am having an issue with inserting words with special characters into my database. The word seems to get truncated at the special character.

I am using MySQL 5.1.41 the collation on my database is utf8_general_ci. I am using PDO to facilitate database interaction.

Here is an example of what I am doing.

//$db is a PDO object

$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

//word with a special character

$word = "pépite";

$sql = "INSERT INTO keyword(key_name) VALUES(?)";

$stmt = $db->prepare($sql);

$stmt->execute(array($word));

When this executes I just get "p" in my database, it seems the character é and everything after it is truncated. I'm not sure what I am doing wrong here. If anyone could suggest what I am doing incorrectly it would be very much appreciated. Thanks!

解决方案

Try this

$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

$word = utf8_encode('pépite');

$sql = "INSERT INTO keyword(key_name) VALUES(?)";

$stmt = $db->prepare($sql);

$stmt->execute(array($word));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值