0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Lachie
  • 1,321
  • 1
  • 10
  • 26
  • 1
    Carefully read the content of `$sql` and `bindParam()` again. – Cobra_Fast Mar 19 '18 at 23:15
  • @Cobra_Fast My mistake, I changed that part while adding into StackOverflow. Issue still exists with that fixed. – Lachie Mar 19 '18 at 23:17
  • 2
    You'll probably get some value out of reading this thread: https://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition – e_i_pi Mar 19 '18 at 23:21
  • @e_i_pi Looks to be exactly what I wanted to find. I'll have a read through now. Thanks – Lachie Mar 19 '18 at 23:25

0 Answers0