1

在下面的代码中,我试图将标签(取自用户)绑定到我的 sql 查询。

我会让他们像这种格式一样查询:"tag1","tag2","tag3"

$tagsinput = trim($_GET["q"]);
$exploded_q = explode(",", trim($_GET["q"])); //seperate with comma, remove whitespace
$combined_tags = "\"" . implode("\",\"",$exploded_q) . "\""; //after this tags will be "tag1","tag2"...

$comb_leng = count($exploded_q);

$stmt = $db->prepare("SELECT * FROM posts a JOIN (SELECT p.id FROM tags_posts tp JOIN posts p ON tp.id_post = p.id JOIN tags t ON tp.id_tag = t.id WHERE t.name IN (:tags) GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :howmany ) q ON a.id = q.id");
$stmt->bindValue(":tags", $combined_tags, PDO::PARAM_STR);
$stmt->bindValue(":howmany", count($exploded_q), PDO::PARAM_STR);
$stmt->execute();
$post_result=$stmt->fetchAll();

如果我在没有 bindValue 查询的情况下执行此操作,它将按预期工作。但是如果我用 绑定它们bindValue(),代码就不起作用。Sql 返回空结果。

4

0 回答 0