1
I need to create a simulation that will make a 5 question SELECT randomly.
I’m using the ORDER BY RAND()
, but the results are repeated, and I don’t know how I can fix it.
I even created an array that stored the code of each question, but I don’t know how to compare it when doing the SELECT
.
Does anyone know how to fix this problem?
The code:
$con = conectar();
$x=1;
for($i=$x; $i<=$x+4; $i++){
$sql = "SELECT * FROM simulado where cod_disc like 1 order by RAND()";
$res = mysqlexecuta($con,$sql);
$row = mysql_fetch_array($res);
echo $i . ") " . $row['enunciado'];
$questoes[$i] = $row ['cod_questao'];
}
*The mysqlexecuta method only executes the query and checks for any error in the execution.
ORDER BY RAND itself does not repeat any data. Basically it’s a matter of taking the query out of the loop, because the problem is that you make several Selects and each SELECT you’re reworking the RAND unnecessarily (and changing the sequence, which causes repetition). With a SELECT only multiple records without repeating, just repeat the mysql_fetch_array to get the next one. Besides, your loop is a little complex, so what’s the use of the X? It looks like some kind of pagination, but if so, it needs [Edit] and for the whole problem, as it will also affect the RAND issue
– Bacco
Redo the logic of loop for(). Run this query several times straight on your sgbd console and see that Rand() will never repeat itself.
– Bleno Lopes