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. 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; 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']; 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()); 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 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()); 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 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
Archived
This topic is now archived and is closed to further replies.