Randomly select 5 records from a db
| From: | Ben Weinberger | Date: | Mon, 02 Oct 2000 22:11:04 +0000 |
| Subject: | Randomly select 5 records from a db | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18320@lists.php.net to get a copy of this message | ||
Hi~
I am constructing part of a PHP4 page that I would like to randomly select 5 records from my MySQL db and display them. I know how to do the standard selects and display the data, but nothing random. The database lists benefits of being a member of our association, and we would like to display different ones each time the query is performed-- is there a way to randomly query the db for 5 records?
I tried using the following but it didn't work....
$sql= "SELECT * from meminfo where type='bene'";
$result= mysql_query($sql);
while ($myrow= mysql_fetch_array($result)) {
$benefits=$myrow["title"];
print "$benefits<BR>"; //this is just to test to make sure it's pulling the data I want
srand ((double) microtime() * 10000000);
$input = array ("$benefits", 5);
$rand_keys = array_rand ($input);
print $input[$rand_keys[0]]."\n";
print $input[$rand_keys[2]]."\n";
print $input[$rand_keys[3]]."\n";
print $input[$rand_keys[4]]."\n";
print $input[$rand_keys[5]]."\n";
}
Thanks,
-Ben