In the following script, I've bound all the parameters using positional offsets to handle the IN
clause in PDO. $_POST["group"]
is an array. The Chrome Console shows that there are 12 values in the array in Form Data. The number of question marks in the In Clause also corresponds to that of the values. Everything seems to be set fine.
SELECT a.id,a.likes,a.dislikes from `like_dislike` a
INNER JOIN `model_no` b ON a.id = b.id
WHERE b.model_no IN (?,?,?,?,?,?,?,?,?,?,?,?)
But I have no idea why the script can only return six of the results.
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
I've also run the query in phpmyadmin and there's no problem with the query. Can anyone see if there's anything wrong during the binding process?
$dbh = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);
$id_group = $_POST["group"];
$in = str_repeat('?,', count($id_group) - 1) . '?';
$sql = "SELECT a.id,a.likes,a.dislikes from `like_dislike` a
INNER JOIN `model_no` b ON a.id = b.id WHERE b.model_no IN ($in)";
$users = $dbh->prepare($sql);
$i = 1;
foreach ($id_group as $id) {
$users->bindValue($i++, $id);
}
$users->execute($id_group);
$rows = $users->fetchAll();
foreach($rows as $row)
{
echo "<div id='tid_".$row['id'].">".$row['likes']." likes, ".$row['dislikes']." dislikes</div>";
}