gpt4 book ai didi

php - 如何编写未序列化类别的查询

转载 作者:行者123 更新时间:2023-11-29 12:56:42 26 4
gpt4 key购买 nike

所以我的函数 post() 做了接下来的事情:

$category = serialize($postdata['post_category']);
$query = "
INSERT INTO posts(post_title, post_content, post_category)
VALUES ('$postdata[post_title]', '$postdata[post_content]', '$category')
";

将序列化数据插入数据库,但在调用函数以仅显示那些具有相同类别的帖子时遇到问题,该函数如下所示:

public function cat($cat){
global $db;
$query = "
SELECT * FROM posts
WHERE post_category LIKE '%$cat%'
";
return $db->select($query);
}

所以我的问题是:对于这种特殊情况,最好将未序列化的数据插入数据库(以及如何做到这一点),还是提取序列化数据然后将其反序列化以显示一些特定的帖子(与之前的相同,尝试了几种选择,但没有获得正确的结果)?

最佳答案

If you want to store the categories array, you can use implode(',', $postdata['post_category'])

$category = implode(',', $postdata['post_category']);
$query = "
INSERT INTO posts(post_title, post_content, post_category)
VALUES ('$postdata[post_title]', '$postdata[post_content]', '$category')
";

and if you want to search the post based on some category you can use

public function cat($cat){
global $db;
$query = "
SELECT * FROM posts
WHERE find_in_set(".$cat.",post_category)
";
return $db->select($query);
}

关于php - 如何编写未序列化类别的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com