Php 使用%C3特殊字符时,数据项mySQL被截断
我有以下疑问:Php 使用%C3特殊字符时,数据项mySQL被截断,php,mysql,utf-8,character-encoding,special-characters,Php,Mysql,Utf 8,Character Encoding,Special Characters,我有以下疑问: INSERT INTO `poll` (`pollId`, `groupId`, `creatorUserId`, `createdDateTime`, `lastVoteDateTime`, `pollQuestion`, `pollOption`) VALUES (NULL, '24', '73', '2014-02-18 21:27:12', '0000-00-00 00:00:00',
INSERT INTO `poll` (`pollId`, `groupId`, `creatorUserId`, `createdDateTime`,
`lastVoteDateTime`, `pollQuestion`, `pollOption`)
VALUES (NULL, '24', '73', '2014-02-18 21:27:12', '0000-00-00 00:00:00',
'1111111111', '222222222222Ã223442')
当使用php执行此查询时,插入的pollOption的数据会被截断,如下所示,没有Ã223442强>
但在myManager中执行时,pollOption的数据成功插入“22222Ã223442”强>
注:
数据库、表和字段都是UTF-8。
php文件是ANSI编码
PHP代码:
date_default_timezone_set('Asia/Calcutta');
require_once "config.php";
error_reporting(0);
$date=date("Y-m-d H:i:s");
$response="";
if(isset($_REQUEST['userId']))
{
$userId=$_REQUEST['userId'];
$groupId=$_REQUEST['groupId'];
$pollQuestion=$_REQUEST['pollQuestion'];
$pollOptions=rawurldecode(htmlspecialchars($_REQUEST['pollOptions']));
$insertPoll=mysql_query("INSERT INTO `poll`
(`pollId`, `groupId`, `creatorUserId`,
`createdDateTime`, `lastVoteDateTime`, `pollQuestion`, `pollOption`)
VALUES (NULL, '$groupId', '$userId', '$date', '0000-00-00 00:00:00',
'$pollQuestion', '$pollOptions')") or die(mysql_error());
echo("INSERT INTO `poll`
(`pollId`, `groupId`, `creatorUserId`, `createdDateTime`,
`lastVoteDateTime`, `pollQuestion`, `pollOption`)
VALUES (NULL, '$groupId', '$userId', '$date', '0000-00-00 00:00:00',
$pollQuestion', '$pollOptions')");
if($insertPoll)
{
$response.='{"success":"0","message":"successfully poll posted"}';
}else
{
$response.='{"success":"1","message":"something went wrong"}';
}
echo $response;
}您的php文件中的Ã肯定是拉丁语-1Ã而不是UTF-8Ã。当使用拉丁-1扩展字符时,linux mysql在发现第一个不正确的字符时只会截断,而windows mysql会给您一个错误 如果是拉丁语-1,请在您的违规字段中使用utf8_编码:
$insertPoll=mysql_query("INSERT INTO `poll`
(`pollId`, `groupId`, `creatorUserId`,
`createdDateTime`, `lastVoteDateTime`, `pollQuestion`, `pollOption`)
VALUES (NULL, '$groupId', '$userId', '$date', '0000-00-00 00:00:00',
'$pollQuestion', '".utf8_encode($pollOptions)."')") or die(mysql_error());
编辑:再调查一下,错误并不取决于linux/windows,而是取决于是否启用了严格模式。对不起
无论如何,使用正确的UTF-8将解决您的问题