我正在制作某种测验项目,我想从数据库中选择并显示随机行,但仅限从 ID 5 到 ID 25 的行,以便显示 20 个问题并将状态从不显示更新为显示。
$stmt = $DB_con->prepare('SELECT * FROM questions_Library (no idea)');
$stmt->execute();
if($stmt->rowCount() > 0){
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo $row['question'];
}
}
最佳答案
那么您应该将结果集限制为 20。
$stmt = $DB_con->prepare('SELECT * FROM questions_Library LIMIT 20');
$stmt->execute();
if($stmt->rowCount() > 0){
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo $row['question'];
}
}
//Update the state of questions returned to displayed
您可能还想仅选择那些尚未显示的内容
$stmt = $DB_con->prepare('SELECT * FROM questions_Library WHERE displayed = 0 LIMIT 20');
$stmt->execute();
if($stmt->rowCount() > 0){
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo $row['question'];
}
}
//Update the state of questions returned to displayed
关于php - 使用 PHP 从 SQL 中选择随机行,但从特定点到点 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57138591/