-4

我想从名为 questions 的表中获取随机 ID 并显示随机行,使用此代码时出现错误

Warning: mysql_fetch_array() expects parameter 1 to be resource

我已经使用 min 和 max 函数来获得随机这里是代码 plz iam 需要它

<?php 

session_start();
require_once("scripts/connect_db.php");
$arrCount = "";
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysql_query("SELECT MIN(id) as min_id , MAX(id) as max_id FROM questions ");
$row = mysql_fetch_array(rand($sql['min_id'],$sql['max_id']) ) or die($row."<br/>      <br/>".mysql_error());

$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question'  LIMIT 1");
    while($row = mysql_fetch_array($singleSQL)){
        $id = $row['id'];
        $thisQuestion = $row['question'];
        $type = $row['type'];
        $subject =$row['subject'];
        $exam =$row['exam'];
        $explan =$row['explan'];
        $question_id = $row['question_id'];
        $s ='<strong>'.$subject.'</strong>';
        $e ='<small>'.$exam.'</small>';
        $q = '<h2>'.$thisQuestion.'</h2>';
        $ex ='<p class="exp">'.$explan.'</p>';
        $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 .= '<table class="table table-hover table-   bordered"> <tr>
            <td><label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label></td>
            </tr></table>
            <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />
            ';

        }
        $output = ''.$s.','.$e.''.$q.','.$answers.''.$ex.' <span id="btnSpan"><button onclick="post_answer()" id="show">Submit</button></span>';
        echo $output;
       }
    }


?>

提前谢谢

4

3 回答 3

2

似乎您正试图直接从$sqlvalue 读取数据,这只不过是指向结果集的指针。

此外,我不确定您在当前实现中使用这些值做什么。您正在尝试进行查询并将值设置为$row,但是在下一个查询之后您甚至在使用这些值之前立即覆盖$row,所以我不确定您甚至查询的是什么。

你应该有这样的东西:

$sql = mysql_query("SELECT MIN(id) as min_id , MAX(id) as max_id FROM questions ");
$row = mysql_fetch_array($sql);

$min_id = $row['min_id'];
$max_id = $row['max_id'];

我还应该注意,您应该考虑使用mysqliorPDO代替mysql_*函数,因为这些函数已被弃用。此外,我还建议为您的查询添加错误处理,以便涵盖所有可能的返回值。

于 2013-08-06T19:52:26.630 回答
1

从文档:

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

相反,您通过了:

rand($sql['min_id'],$sql['max_id']) 

这是一个整数

于 2013-08-06T19:51:40.493 回答
1

The rand() function generates a random integer. If this function is called without parameters, it returns a random integer between 0 and RAND_MAX.

so you are giving mysql_fetch_array() an integer value instead of the result object.

于 2013-08-06T19:59:13.557 回答