On one page I would like to get 3 random rows from my db and echo them on my page. But I’m not sure on how to do this. My code isn’t showing anything, so I guess there is something wrong. I am using code I have in other pages and just modified bits and pieces, but I guess I lost something on the way…
I tried the following:
<?php
// Number to display
$num = 3;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM members WHERE amount > numbers AND active = 1 ORDER BY RAND() LIMIT $num");
// For all the rows that you selected
while ($row = mysql_fetch_array($result))
echo $row['member_id'];
?>
Avoid pointless variables like $num;, and useless comments.
Don’t put in a closing ?> tag.
Always put {} brackets in, even if they aren’t needed.
Format your SQL.
<?php
$result = mysql_query ("SELECT *
FROM members
WHERE amount > numbers
AND active = 1
ORDER BY RAND()
LIMIT 3");
while ($row = mysql_fetch_array($result))
{
echo $row['member_id'];
}
“ORDER BY RAND()” will make the rows random like you wanted. However if nothing is showing, its because the query returned no rows.