1

In below code, I'm trying to bind tags (which is taken from user) to my sql query.

I'll put them to query like this format: "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();

If I execute this without bindValue my query, it works as I expected. But if I bind them with bindValue(), code doesn't works. Sql returns empty result.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
alayli
  • 325
  • 1
  • 6
  • 18

0 Answers0