PDO准备插入序列化数据错误
我正在尝试使用 PDO 将序列化数据插入到 mySQL 中,但遇到了一些语法错误。我是不是错过了什么?
一些简化的编码:
$test['1'] = "one";
$condition = serialize($test);
$stmt = $dbh->prepare("INSERT INTO weather(condition) VALUES (:condition)");
$stmt->bindParam(":condition",$condition);
$stmt->execute();
$stmt->debugDumpParams()
显示
SQL: [67] INSERT INTO 天气(条件)
值(:条件)参数:1 键: 名称:[10]:条件参数号=-1 名称=[10]“:条件”is_param=1 param_type=2
print_r($stmt->errorInfo())
显示
数组 ( [0] => 42000 [1] => 1064 [2] =>您的 SQL 语法有错误;检查手册 对应你的MySQL服务器 正确使用语法的版本 接近'条件)值 ('a:1:{i:1;s:3:\"one\";}')' 在第 1 行 )
I am trying to insert a serialize data into mySQL using PDO and I'm hitting some syntax error. Have I missed out something?
Some simplified coding:
$test['1'] = "one";
$condition = serialize($test);
$stmt = $dbh->prepare("INSERT INTO weather(condition) VALUES (:condition)");
$stmt->bindParam(":condition",$condition);
$stmt->execute();
$stmt->debugDumpParams()
shows
SQL: [67] INSERT INTO weather(condition)
VALUES (:condition) Params: 1 Key:
Name: [10] :condition paramno=-1
name=[10] ":condition" is_param=1
param_type=2
print_r($stmt->errorInfo())
shows
Array ( [0] => 42000 [1] => 1064 [2]
=> You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server
version for the right syntax to use
near 'condition) VALUES
('a:1:{i:1;s:3:\"one\";}')' at line 1
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决办法其实很简单。
Condition
是MySQL中的保留字。您不能将其用作列的名称。您可以在其网页 此处< /a> 和 此处。
Solution is in fact very simple.
Condition
is reserved word in MySQL. You cannot use it as name of your column.You can find full list of reserved words for MySQL on their webpage here and here.
当你专门将它定义为字符串时会发生什么?
What happens when you define it as a string specifically?