在下面的脚本中,我使用位置偏移来绑定所有参数来处理PDO中的IN子句. $_POST [“group”]是一个数组. Chrome控制台显示表单数据中的数组中有12个值. In Clause中的问号数量也与值的数量相对应.一切似乎都很好.
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 (?,?,?,?,?,?,?,?,?,?,?,?)
但我不知道为什么脚本只能返回六个结果.
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
0 likes, 0 dislikes
我也在phpmyadmin中运行查询,查询没有问题.
任何人都可以看到绑定过程中是否有任何错误?
$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>";
}
最佳答案 我想我得到了解决方案,
foreach($rows as $row)
{
echo "<div id='tid_".$row['id'].">".$row['likes']." likes, ".$row['dislikes']." dislikes</div>";
^--this quote ^--you forgot to end here.
}
固定代码
我建议在使用string连接数组/变量时使用{}.它更易读,更容易理解.
echo "<div id='tid_{$row['id']}'>{$row['likes']} likes, {$row['dislikes']} dislikes</div>";