This question already has an answer here:
I want to get random Id's from my table called questions and display random rows , when use this code i get an error
Warning: mysql_fetch_array() expects parameter 1 to be resource
I have used min and max functions for getting random here is the code plz iam in need of it
<?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;
}
}
?>
thanx In advance
</div>
From the docs:
array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
Instead you passed in:
rand($sql['min_id'],$sql['max_id'])
which is an int
It seems that you are trying to read data directly from $sql
value which is nothing other than a pointer to a result set.
Also, I am not sure what you are even doing with these values in your current implementation. You are attempting to make a query and set values to $row
, but then you promptly overwrite $row
after the next query before you even use the values, so I am not sure what you are even querying for.
You should have something like this:
$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'];
I should also note that you should look to use mysqli
or PDO
instead of mysql_*
functions as these are deprecated. Additionally, I would also suggest, adding error handling for your queries so that you have all possible return values covered.
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.