w0rtel Posted January 13, 2003 Share Posted January 13, 2003 I want to get a random row from my table. And i use this: $sql = "SELECT * FROM random ORDER BY RAND() LIMIT 1"; $result = mysql_query($sql); $object = mysql_fetch_object($result); $rid = $object->rid; But I always get the first row. Never a different one.. Why? (MySQL v. MySQL 3.23.54-nt) Thnx 4 the help.. Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/ Share on other sites More sharing options...
pallevillesen Posted January 14, 2003 Share Posted January 14, 2003 I want to get a random row from my table. And i use this: $sql = "SELECT * FROM random ORDER BY RAND() LIMIT 1"; $result = mysql_query($sql); $object = mysql_fetch_object($result); $rid = $object->rid; But I always get the first row. Never a different one.. Why? (MySQL v. MySQL 3.23.54-nt) Thnx 4 the help.. Probably because you\'re opening a new connection each time.. The rand() function is not truly random, so you need to give it a near random seed, i.e. use rand(seedvalue)... You may use something like rand(seconds(curdat())) or some other stuff. If you\'re using php you could take a variable and assign a random number to it and them use that as seed in you query. P., denmark Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-105 Share on other sites More sharing options...
axyonline Posted February 15, 2003 Share Posted February 15, 2003 may be u can random selection by doing that: get the last record from table ( u can do that \"select last_insert_id()\" ) then $min=1 ; $max=$last_record_id ; $random_record=rand($min,$max); then u print the random row by mysql_fetch_row.. if u have 10000 records in your table it will search all the records from table then randomize 1 row. searching is too late. but doing like this searching is quite time and it is easy for me see u later Cihan Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-456 Share on other sites More sharing options...
dmeade Posted February 23, 2003 Share Posted February 23, 2003 thanks to the information above I managed to get a good solution: [php:1:d09dd98aa6]<?php $random_seed=rand(1, time()); $query = \"SELECT * FROM $table ORDER BY RAND($random_seed) limit 1\"; ?>[/php:1:d09dd98aa6] Thanks all. - Dave Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-508 Share on other sites More sharing options...
sohaibshaheen Posted August 29, 2010 Share Posted August 29, 2010 thanks to the information above I managed to get a good solution: [php:1:d09dd98aa6]<?php $random_seed=rand(1, time()); $query = \"SELECT * FROM $table ORDER BY RAND($random_seed) limit 1\"; ?>[/php:1:d09dd98aa6] Thanks all. - Dave What if the rand output doesnot equal row value.. It may produce 100 when u have 30 results only... Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-1104793 Share on other sites More sharing options...
Pikachu2000 Posted August 29, 2010 Share Posted August 29, 2010 Google "mysql why order by rand is bad". Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-1104894 Share on other sites More sharing options...
fenway Posted August 30, 2010 Share Posted August 30, 2010 Google "mysql why order by rand is bad". Or read the stickies. Quote Link to comment https://forums.phpfreaks.com/topic/43-rand-always-returns-the-same/#findComment-1105227 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.