I just have a quick question about grabbing a random row from a mysql table.. how do you do it?
I have tried "ORDER BY rand()" but it keeps picking the same row for some reason (maybe that reason can be explained).
Any suggestions would be greatly appreciated.
Thanks a lot,
Then select the row using the random number generated:
SELECT * from TABLE WHERE RowID = '$randnumber'
RowID should be some sort of auto incremented field using to identify rows. The 'NUMBER' above should be changed to the total number of rows in your table to avoid creating a random number which is more than your number of rows.
Ok,
If I use php to generate a random number, and then grab that number from an autoincremented column on the table, wont that mess up if the number isnt there? It is grabbing information that will be added and deleted as time goes on. So autoincremented numbers will be lost here and there. Ex: The number of rows may be 30, but the highest autoincrement may be 50, because 20 rows have been deleted.
I hope that makes sense. If so, how can I fix that part of it?
Thanks a lot,
I am running version 3.23.52.
ORDER BY RAND() keeps grabbing the same row for some reason.
But, when I try to grab a random row from another table, it seems to work fine..
Weird.. lol.
Matthew
Last edited by nurseryboy; 11-07-2002 at 03:11 AM..
Haha, yeah, I thought of that.
The fist thing I did was make sure I had at least two rows. That would be kinda dumb to be expecting something that wasnt there.. lol.
Good suggestion though. Keeping me on my feet.
Ok, I added a few more and it seems to work. It sticks to one row more than the others, but overall they are random.
Thanks a lot for the help, I think that was the problem.
Glad we could help.... if you have two rows it could always go to the same row because its a 50/50 chance with 10 rows its a 10/90 chance... so more chance of getting another one!