Select unseen random row from MySQL table
Posted
by
user1476925
on Stack Overflow
See other posts from Stack Overflow
or by user1476925
Published on 2012-06-23T14:36:03Z
Indexed on
2012/06/23
15:16 UTC
Read the original article
Hit count: 158
We have a list of questions in a MySQL database and want it to show a random approved question to the user. When you click the Random button, we want another random question to be shown, but not any of the ones the user has already seen.
Right now the script looks like this:
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("aldrig") or die(mysql_error());
$result = mysql_query("SELECT * FROM spg WHERE approved='1' ORDER BY RAND() LIMIT 1;")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo "<div class='contentTitle'><h1>";
echo $row['text'];
echo "</h1></div>";
}
?>
© Stack Overflow or respective owner