我收到一个"不正确的字符串值"使用php插入MySql 5.6数据库时出错(我从多个服务器尝试了多个版本的PHP,包括v5.5 - 所有Windows机器)。
我已经搜索了论坛并尝试了一切,但似乎无法弄清楚这一点!以下是详细信息。我希望我只是遗漏了一些简单的东西,所有的帮助都非常感谢。先感谢您!
=============================================== ======================
当前的MySQL 5.6配置(相关部分):
[client]
default-character-set = utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
验证服务器设置:
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
---------------------------------------------------
Variable_name | Value
---------------------------------------------------
character_set_client | utf8mb4
---------------------------------------------------
character_set_connection | utf8mb4
---------------------------------------------------
character_set_database | utf8mb4
---------------------------------------------------
character_set_filesystem | binary
---------------------------------------------------
character_set_results | utf8mb4
---------------------------------------------------
character_set_server | utf8mb4
---------------------------------------------------
character_set_system | utf8
---------------------------------------------------
collation_connection | utf8mb4_general_ci
---------------------------------------------------
collation_database | utf8mb4_general_ci
---------------------------------------------------
collation_server | utf8mb4_unicode_ci
---------------------------------------------------
创建数据库语句(这是我严格创建的测试数据库,用于解决此问题):
CREATE DATABASE `chartest` /*!40100 COLLATE 'utf8mb4_general_ci' */
创建表格声明:
CREATE TABLE IF NOT EXISTS `testtable` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`testcol` text COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
测试/示例PHP代码: (一个注意事项 - 我知道我们应该使用PDO或MySQLi,我们正在进行迁移/升级,但我们有很多遗留代码,因此这个问题需要在此期间/短期内使用现在已弃用的mysql函数)。
<?php
//Establish DB connection
$DBConnection = mysql_connect('my_db_server_IP', 'my_db_username', 'my_db_password');
if (!$DBConnection) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('chartest', $DBConnection)) {
echo 'Could not select database';
exit;
}
//Set misc charset options in mysql
/*Note: these should not really be needed given the
character-set-client-handshake = FALSE setting in the MySQL config file.
Also, I have tried every combination of these, building
from the ground up - no luck.
I have left these in here just for the sake of being thorough */
mysql_query("SET character_set_client=utf8mb4", $DBConnection);
mysql_query("SET character_set_connection=utf8mb4", $DBConnection);
mysql_query("SET character_set_database=utf8mb4", $DBConnection);
mysql_query("SET character_set_results=utf8mb4", $DBConnection);
mysql_query("SET character_set_server=utf8mb4", $DBConnection);
mysql_set_charset("utf8mb4", $DBConnection);
mysql_query("SET CHARACTER SET utf8mb4", $DBConnection);
mysql_query("SET NAMES utf8mb4", $DBConnection);
//The query below contains a "middle dot" which is what causes the error (or at least is an example)
$SQLQuery = "INSERT INTO testtable (testcol) VALUES ('test · middle dot insert')";
//Execute query
$result = mysql_query($SQLQuery, $DBConnection);
if (!$result)
{
echo mysql_error();
exit;
}
else
{
echo 'worked - '. mysql_insert_id();
}
mysql_close($DBConnection);
?>
错误返回
字符串值不正确:&#39; \ xB7中间点插入&#39;对于专栏&#39; testcol&#39;在第1行
...再次感谢你的智慧。
=============================================== ===================
编辑RE:重复问题:我已经审核了建议的重复问题/答案UTF-8 all the way through,但我看不出我错过了什么(虽然我显然必须遗漏一些东西!!)。
更具体地说(根据引用的帖子),我的数据存储&#39;是utf8mb4,我的数据访问&#39;正在使用mysql_set_charset,我实际上并没有尝试输出&#39;这些字符(至少这不是我的问题),字符/ SQL查询是硬编码的 - 因此这个问题与&#39;输入&#39;无关。任何数据。最后,关于其他考虑因素&#39;我没有使用任何字符串函数,所以这不是我能说的最好的问题。
再一次,非常感谢任何反馈。谢谢。