gpt4 book ai didi

php - MySQL随机抽取题

转载 作者:行者123 更新时间:2023-11-29 02:11:49 24 4
gpt4 key购买 nike

我找不到我需要的东西。我试图创建 at quizzer 从数据库中提取问题。目前,它需要数据库中的所有问题以及我需要它随机抽取 10 个问题。我刚刚开始学习如何编码。

这是我的问题代码和答案:

<?php
//Set question number
$number = (int) $_GET['n'];

/*
* Get total questions
*/
$query = "SELECT * FROM `klausimai`";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;

/*
* Get Question
*/
$query = "SELECT * FROM `klausimai`
WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
* Get Choices
*/
$query = "SELECT * FROM `atsakymai`
WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>

最佳答案

如前所述,使用SQL LIMIT语句($query = "SELECT * FROM klausimai LIMIT 10";)限制klausimai查询结果为10条:

<?php
//Set question number
$number = (int) $_GET['n'];

/*
* Get total questions
*/
$query = "SELECT * FROM `klausimai` LIMIT 10";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;

/*
* Get Question
*/
$query = "SELECT * FROM `klausimai`
WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
* Get Choices
*/
$query = "SELECT * FROM `atsakymai`
WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>

关于php - MySQL随机抽取题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50227020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com