programguru Posted February 20, 2009 Share Posted February 20, 2009 does anyone have an idea on selecting random records.. my code is working fine as is. as you can see my mysql query is limited to 3 WHERE featured=1, but it's possible i will have 20-30 featured records. i tried variations of php rand() and its family with no luck.. any ideas? ... $sql = "SELECT id, date_added, date_start, date_end, title, spaw1, featured FROM $table_name WHERE featured=1 ORDER by date_added LIMIT 3"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $row['id']; $date_added = $row['date_added']; $date_start = $row['date_start']; ... Quote Link to comment https://forums.phpfreaks.com/topic/146157-solved-randomly-select-3-of-n-records/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 $sql = "SELECT id, date_added, date_start, date_end, title, spaw1, featured FROM $table_name WHERE featured=1 ORDER by rand() LIMIT 3"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $row['id']; $date_added = $row['date_added']; $date_start = $row['date_start']; That would work. Mysql has it's own rand() function. Putting that in the order by should randomize the order/results. Quote Link to comment https://forums.phpfreaks.com/topic/146157-solved-randomly-select-3-of-n-records/#findComment-767329 Share on other sites More sharing options...
programguru Posted February 20, 2009 Author Share Posted February 20, 2009 premiso, i did not realize mysql had that function as well! that worked perfect. Quote Link to comment https://forums.phpfreaks.com/topic/146157-solved-randomly-select-3-of-n-records/#findComment-767341 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.