【发布时间】:2012-01-17 16:24:37
【问题描述】:
这是我的代码我没有看到我的代码有什么问题,但我不明白为什么它不会保存在数据库中。
我的阵列详情
Array
(
[dates] => Array
(
[0] => 2012-01-18
[1] => 2012-02-18
[2] => 2012-03-18
[3] => 2012-04-18
[4] => 2012-05-18
)
[amount] => Array
(
[0] => 2000
[1] => 2000
[2] => 2000
[3] => 2000
[4] => 2000
)
[deposit] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
)
我的保存代码
$sql = "INSERT INTO transaction (id, details)
VALUES('$id', $serializeddetails)";
$query = mysql_query($sql) or die("Fatal error: ".mysql_error());
我收到了这个错误
Fatal error: 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 'a:3:{s:5:"dates";a:6:{i:0;s:10:"2012-01-18";i:1;s:10:"2012-02-18";i:2;s:10:"2012' at line 2
我使用 longtext 作为我的数据类型并尝试 longblob 但没有希望,
我也尝试使用 base64_ecode();
$serializeddetails = base64_encode(serialize($detailsarray));
得到了这个错误
Fatal error: 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 ')' at line 2
我的代码有什么问题,为什么不能保存到数据库中?
谢谢
【问题讨论】:
-
您是否忘记转义值?此外,您的第一个示例实际上并没有序列化任何内容... FWIW,如果您
json_encode()它,这些数据可能会以更紧凑且与语言无关的方式存储。 -
请记住,您不应将序列化数据保存在数据库中,请查看Database normalization article on wikipedia 以了解您应该拥有哪些表。我知道这是一种将数据保存在一个字段中的快速解决方案,但是当您想要阅读诸如“向我显示给定日期的交易”或“用户每月完成多少交易”之类的棘手内容时就会出现问题.
-
@Progman:一般来说你是对的,但你不应该永远这样做 - 存储序列化数据有多种原因。
标签: php mysql serialization