The trouble I am having is that I am getting no response when I try to pass the variable binded into the query (stored in a variable $searchString
).
The variable $searchString = 'extreme', 'water'
My PDO/MySQL statement:
$sql = "SELECT *
FROM posts
INNER JOIN post_tags ON posts.id = post_tags.postid
INNER JOIN tags ON post_tags.tagid = tags.id
WHERE tags.tag IN ( :searchTags )
AND posts.suspended = 0
GROUP BY posts.id";
$results = $db->prepare($sql);
$results->bindParam(':searchTags', $searchString);
$results->execute();
while ($row = $results->fetch()) {
echo $row[0]; // Should be the first column which is "id"
}
Expected output: 1
(in my database there is only one item that matches the query and its id is 1)
I suspect the bindParam() function messes with the value of $searchString
as if I remove the bindParam() line and put the variable directly into the SQL query it works, like so:
$sql = "SELECT * FROM posts INNER JOIN post_tags ON posts.id = post_tags.postid INNER JOIN tags ON post_tags.tagid = tags.id WHERE tags.tag IN ({$searchString}) AND posts.suspended = 0 GROUP BY posts.id";
Note: the variable $searchString
must have single quotes '
for the SQL query to work