Print random string from mysql query

I was looking for a way to create a collaboration widget. So I have a mysql database and a table (called translations) and a little script to allow users to translate one page at a time.

But I'm not quite sure about my script. I do not think this is effective enough. First, mysql retrieves all rows with an empty "en" column, and then one of them appears across the screen after a while. Is there any other way to do this? This is the code:

//Retrieve all the data from the "translations" table
$result = mysql_query("SELECT * FROM translations WHERE en IS NULL OR en=''") or die(mysql_error());

$Randnum=rand(0,mysql_num_rows($result)-1); //Gets a random number between 0 and the maximum number of rows
$i=0;   //Start to 0
while($Col = mysql_fetch_array($result))  //While there are rows to evaluate
    {
    if ($i==$Randnum)
        {
        echo "\"".$Col['es']."\"<br><br>Translate it to English: <br>";
        }
    $i++;
    }

- "echo $Col [$ Randnum] ['es']" "echo $Col. $Randnum ['es']" while while . ? . script $Col 'en' col, ! ( , , ). 'en' - , , . , ORDER BY.

+5
3

ORDER BY RAND() LIMIT 1 .

+7

SELECT * FROM translations WHERE en IS NULL OR en='' ORDER BY rand() LIMIT 0,1
+2

.

@ThiefMaster , "order by rand()" . , , , . , - - !

; ; , , .

, :

mysql_query('SELECT @count := COUNT(*) FROM translations WHERE en IS NULL OR en=''');
mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
mysql_query('PREPARE mystatement FROM "SELECT * FROM translations WHERE en IS NULL OR en='' LIMIT ?, 1"');
$res = mysql_query('EXECUTE mystatement USING @offset');
$row = mysql_fetch_assoc($res);
print_r($row);
+1

All Articles