在下面的代码中,我正在尝试将标记(取自用户)绑定到我的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返回空结果。