frank_solo Posted August 8, 2012 Share Posted August 8, 2012 I would like this to select a random record from MySQL database table. How could I do this? <?php require_once("../includes/db_config.php"); $db = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if($db) { if(mysql_select_db("", $db)) { $sql = "SELECT id, imageurl1, title, county, rent, description FROM apartments WHERE date_created = (SELECT MAX(date_created) FROM apartments)"; $result_set = mysql_query($sql, $db); if($result_set && mysql_num_rows($result_set) == 1) { $latest_listing = mysql_fetch_assoc($result_set); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/ Share on other sites More sharing options...
Porl123 Posted August 8, 2012 Share Posted August 8, 2012 Add "ORDER BY RAND() LIMIT 1" Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367700 Share on other sites More sharing options...
frank_solo Posted August 8, 2012 Author Share Posted August 8, 2012 Where do I add that statement? Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367702 Share on other sites More sharing options...
Porl123 Posted August 8, 2012 Share Posted August 8, 2012 $sql = "SELECT id, imageurl1, title, county, rent, description FROM apartments WHERE date_created = (SELECT MAX(date_created) FROM apartments) ORDER BY RAND() LIMIT 1"; $result_set = mysql_query($sql, $db); if($result_set && mysql_num_rows($result_set) == 1) { $latest_listing = mysql_fetch_assoc($result_set); } Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367703 Share on other sites More sharing options...
frank_solo Posted August 8, 2012 Author Share Posted August 8, 2012 Thank you Porl123! Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367705 Share on other sites More sharing options...
Porl123 Posted August 8, 2012 Share Posted August 8, 2012 No probs Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367707 Share on other sites More sharing options...
frank_solo Posted August 8, 2012 Author Share Posted August 8, 2012 Wait it is not working. Maybe because "MAX(date_created)"? Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367709 Share on other sites More sharing options...
Pikachu2000 Posted August 8, 2012 Share Posted August 8, 2012 Yes. You're trying to get a random record from a results set that only contains one record. Obviously, the "random" record will always be the only one that was selected. Also, see the link in my signature for reasons why 'ORDER BY RAND()' isn't a good idea, and suggestions on better ways to do it. Quote Link to comment https://forums.phpfreaks.com/topic/266793-from-max-to-random/#findComment-1367710 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.