daveh33 Posted October 22, 2007 Share Posted October 22, 2007 What I am trying to do is do a query on a mysql table e.g SELECT * from table where status='active' - I want it so when e.g 10 records are returned, it would display a randomly selected. I have spent a few hours searching the internet for tutorials but have yet to get one working. I have also noticed different ways in doing it. Can someone let me know some methods which have been successfully used before. Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/ Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 SELECT * from table where status='active' ORDER BY RAND(id) LIMIT 10; Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375576 Share on other sites More sharing options...
daveh33 Posted October 22, 2007 Author Share Posted October 22, 2007 Can you tell me where I am going wrong with this code } else { $result = mysql_fetch_row(mysql_query("SELECT * from promotions where used='n' ORDER BY RAND(id)") or die(mysql_error())); $row = mysql_fetch_array( $result2 ) or die(mysql_error()); } $title = $row['title']; Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375603 Share on other sites More sharing options...
sasa Posted October 22, 2007 Share Posted October 22, 2007 change line $result = mysql_fetch_row(mysql_query("SELECT * from promotions where used='n' ORDER BY RAND(id)") or die(mysql_error())); to $result = mysql_query("SELECT * from promotions where used='n' ORDER BY RAND()") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375605 Share on other sites More sharing options...
daveh33 Posted October 22, 2007 Author Share Posted October 22, 2007 OK I changed that and get the below error: - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in members.php on line 52 line 52 been the $row line Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375606 Share on other sites More sharing options...
pocobueno1388 Posted October 22, 2007 Share Posted October 22, 2007 You need to use the variable $result, not $result2 $row = mysql_fetch_array($result)or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375610 Share on other sites More sharing options...
daveh33 Posted October 22, 2007 Author Share Posted October 22, 2007 Excellent that works! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/74335-solved-whats-the-best-way-to-display-a-random-record-from-a-mysql-table/#findComment-375613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.