Ho打印PDO会导致while循环,但来自用户指定的索引,而不是从开始


Ho to print PDO results in a while loop but from a user specified index and not from start?

我有一个从PDO对象创建的数据库结果集。数组包含所有数据。现在我想打印结果,但我想从特定索引开始打印,而不是从一开始。起始索引是由用户指定的。请不要告诉我修改查询,因为这不是我想要的。此外,我到处找过,没有找到任何解决方案。我简化了我的代码,这样更容易理解,也更容易开门见山。感谢您的帮助。:>)

$res2=$conn->prepare("SELECT COUNT(*) FROM blogs");
$res2->execute();
 while($r=$res2->fetch(PDO::FETCH_BOTH)){
 // I have 37 records in $res2 and want to start echoing from record number 10.
//for example I want to echo out $r['title'] but not from the first but from the 5th or 10th index.
}

使用if()语句和计数器:

$userInput = 5;
$i = 0;
while($r=$res2->fetch(PDO::FETCH_BOTH)){
    if($i >= $userInput){
         // echo your output here
    } 
    $i++;
}