Articles

Random Record from MySQL

December 17, 2006 (php, mysql)


I often have the need to show a single record from a database table at random. I have found this PHP code very useful when using a MySQL database.


$query = "select * from quotes where active=1 order by RAND( ) limit 1;";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
$row = mysql_fetch_array($result);

print "<p>" . $row["quoteCopy"] . "</p>";