反序列化通过mysqli_real_escape_string()和serialize()函数传递的字符串值

时间:2014-06-09 13:41:33

标签: php mysql

我通过PHP中的$postmysqli_real_escape_string()函数传递了serialize()变量,如下所示:

$post = mysqli_real_escape_string($db_connect, serialize($item));

然后我将$post值插入MySQL数据库中。存储的数据类似于数据库中的以下格式:

a:1:{s:9:"title";s:37:"This is just a test -Title";}

a:1:{s:3:"post";s:7369:"This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---This is just a test ---- TEXT---";}

如何在将$post变量插入数据库之前仅生成This is just a test -Title变量的文本值(即a:1:{s:9:"title";s:37:"This is just a test -Title";}而不是{{1}})。

2 个答案:

答案 0 :(得分:3)

您可以直接从数组中取出元素并将其直接插入数据库,而不是序列化数组。别忘了清理你的变量!

mysqli_real_escape_string($db_connect, $item['title']);

答案 1 :(得分:2)

在将信息插入数据库之前,您不需要serialize()变量;你可以逃避价值:

$post = mysqli_real_escape_string($db_connect, $item["title"]);

然后在查询中使用$post来插入值。这将导致在数据库中获得该值。