我通过PHP中的$post
和mysqli_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}})。
答案 0 :(得分:3)
您可以直接从数组中取出元素并将其直接插入数据库,而不是序列化数组。别忘了清理你的变量!
mysqli_real_escape_string($db_connect, $item['title']);
答案 1 :(得分:2)
在将信息插入数据库之前,您不需要serialize()
变量;你可以逃避价值:
$post = mysqli_real_escape_string($db_connect, $item["title"]);
然后在查询中使用$post
来插入值。这将导致在数据库中获得该值。