I have the following code, that outputs all data from the comment field, however i wish for it to output only two and for them to be random, how can i got about doing this? many thanks.
$query = mysql_query("SELECT * FROM comments ");
//query the database
echo "<b>Reviews</b> ";
WHILE($rows = mysql_fetch_array($query)):
$comment_by = $rows['comment_by'];
$comment= $rows['comment'];
echo "<div style ='font:14px/21px Arial,tahoma,sans-serif;color:#cf5c3f </h>'>$comment_by</h>";
echo "<div style ='font:14px/21px Arial,tahoma,sans-serif;color:#ff0000 <h></h>'>$comment<br><br>";
endwhile;
?>
Use:
LIMIT 2: for two records
and
ORDER BY RAND(): for random comments
See official MySQL documentation:
ORDER BY
: https://dev.mysql.com/doc/refman/5.0/en/sorting-rows.htmlRAND()
: https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.htmlLIMIT
: https://dev.mysql.com/doc/refman/5.0/en/select.html