randomly select a table, better way?
| From: | Daniel Bray | Date: | Mon, 01 Sep 2003 22:32:52 +0000 |
| Subject: | randomly select a table, better way? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-161392@lists.php.net to get a copy of this message | ||
Ok, I have looked through the archives on both MySQL and PHP's lists, but
I have not seen this asked or mentioned. Weird, I thought this would be a
common thing. Is there a better way to randomly select a table from a
database, than what I have come up with:
<?php
srand ((float) microtime() * 10000000);
$table_array = array ();
mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("database_name");
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$table_name = mysql_tablename($result, $i);
array_push ($table_array , "$table_name");
}
$rand_keys = array_rand ($table_array, 2);
echo "<br><br>Here is a random table: <b>" .
$table_array[$rand_keys[0]]."</b><br>\n";
mysql_free_result($result);
?>
thanks,
DB