我正在设计一个测试,我想随机显示问题。我在数据库中有50个问题。为此,我写了下面的代码:显示随机问题
$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' ORDER BY Rand()");
while($row = mysql_fetch_array($singleSQL)){
$id = $row['id'];
$thisQuestion = $row['question'];
$type = $row['type'];
$question_id = $row['question_id'];
$q = '<h2>'.$thisQuestion.'</h2>';
$sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
while($row2 = mysql_fetch_array($sql2)){
$answer = $row2['answer'];
$correct = $row2['correct'];
$answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label>
<input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
';
}
$output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
echo $output;
}
在第一线这rand()函数无法正常工作的问题,但同样的功能正在寻找答案(多选题选项随机出现)。 此外,当我在第一线做改变SQL查询我的错误:
警告:
mysql_fetch_array() expects parameter 1 to be resource
boolean given in C:\xampp\htdocs\questions.php on line 36
undefined.
我不知道什么是错的!请帮我解决.. !!
'$ singleSQL =请求mysql_query( “SELECT * FROM问题ORDER BY RAND()”);' 这对于随机选择问题来说还不够吗? – arunrc 2015-03-03 06:48:33
请勿使用'mysql_ *'API。这已被弃用。使用'mysqli_ *'ord pdo和准备好的语句。你在哪里连接到数据库?哪一行是第36行? – Jens 2015-03-03 06:49:35
在'mysql_query()'之后放置'或者死(mysql_error())',以便看到SQL错误消息。 – Barmar 2015-03-03 06:52:26