Get mysql on random

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Get mysql on random

Post by hmsg »

Hello ppl

I need to access the values of a mysql query in random ways, this means, when i do a mysql query for example i have 5 rows, but i only want to access the row 2 and the row 4, how can i access the rows that i want ? I need something like this: $row=mysql_fetch_array($query, $number_register)



With the best regards

Hugo Gomes
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

If you need rows 2 and 4, how's that random? :?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://www.php.net/mysql_data_seek allows you to navigate in the resultset...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You can also ORDER BY RAND() LIMIT 2 in your query.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Go with pickle's idea. That is the best way to get a set number of random rows returned.
Post Reply