所以我想做2个while循环,因为我有2个表。在第一个表格中,我希望得到前5个ID
`$sql = "SELECT * FROM link WHERE `userid`= $uid ORDER by clicks DESC LIMIT 5";`
不要介意Ik声明中的变量是不安全的,所以我稍后会使用预备语句。但是,在第一次它应该给出前5个ID时,在第二个时它会给出5-10。第二个$ sql应该是什么样的?
答案 0 :(得分:0)
你需要像这样使用OFFSET:
SELECT * FROM link WHERE `userid`= $uid ORDER by clicks DESC LIMIT 5 OFFSET 5
答案 1 :(得分:0)
第二个SQL看起来像这样:
<?php
...
$sql2 = "SELECT * FROM link WHERE userid = :uid ORDER by clicks DESC LIMIT 5 OFFSET 5";
$stmt = $pdo->prepare(sql2);
$stmt->execute(['uid ' => $uid]);
...
?>
答案 2 :(得分:0)
尝试这个。
$sql = "SELECT * FROM link WHERE `userid`= '$uid' ORDER by clicks DESC LIMIT 5, 5";
这将从记录号6开始提供5条记录(记录号5不会出现在结果中)
答案 3 :(得分:0)
在1个查询中使用2个循环。不需要额外的查询。
例如在for
循环中:
<?php
$results = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n");
for($i=0; $i<5; $i++) {
echo $results[$i];
}
echo " / ";
for ($i=5; $i<10; $i++) {
echo $results[$i];
}
// yields: abcde / fghij
?>
或while
循环中的相同示例:
<?php
$results = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n");
$i = 0;
while($i < 5) {
echo $results[$i];
$i++;
}
echo " / ";
while($i < 10) {
echo $results[$i];
$i++;
}
// yields: abcde / fghij
?>