Hi php fans!
Can you tell me how can i choose a random value using only the database from mysql table?
I´m asking this my database has unordered id´s like: 1,10, 11,15, and so on...........
Thanks for any help!
Mmmm ... RAND().
Here is an example:
select LINEID from TABLEOFSTUFF order by RAND();
Whenever you want to order the result by some random mess, then use the order by RAND() notation. If you want to read up more on it, check this page:
http://www.mysql.com/doc/en/Mathematical_functions.html#IDX1261
Use Limit:
$r = rand(0, 25); //where 25 is your number of records $sql = "SELECT * FROM table LIMIT $r, 1"; $results = mysql_query($sql) or die(mysql_error());
That will not be dependant on the ID, but instead pull out the nth record generated by the rand() function.
If you just want ONE random entry from the database, then tack this to the end:
LIMIT 1
ie:
select LINEID from TABLEOFSTUFF order by RAND() LIMIT 1;
Thank you for your help!
But It simply doesn´t work!
I need:
get all id´s from mysql table
print random id´s from database.
But You have to see that those id´s aren´t organized.
because some of them were deleted.
Another idea?? :rolleyes:
Thanx!
I don't understand why the suggestions won't work. Could you explain better?
I'm baffled too...
If you want ALL the ids returned from the database into php, then simply drop the "LIMIT" in the qeury. Keep the RAND() if you want it output from the database randomly. Or if you want to use php to randomize them, check out array_shuffle() at php.net.
You were right man!
id works, but I hat to remove limit.
Thanks!
😃 🙂 :p