Got a table of many rows. What is the fastest and less expensive way to pick a random row out of it ?
Thanks
| SitePoint Sponsor |


Got a table of many rows. What is the fastest and less expensive way to pick a random row out of it ?
Thanks
I appreciate the help of everyone at SitePoint.com
This is how I would do it.
PHP Code:$rand_num = rand(1,mysql_num_rows($result));
for($k=0;$k<$rand_num;$k++){
$rand_row = mysql_fetch_array($result);
}
//$rand_row is your random pick.
you can also do this
PHP Code:$result = mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 0,1");
$row = mysql_fetch_array($result);


This works like a charm! thanksOriginally Posted by JRMillion
I appreciate the help of everyone at SitePoint.com
Bookmarks