Re: randomly select a table, better way?

From: Date: Mon, 01 Sep 2003 22:49:55 +0000
Subject: Re: randomly select a table, better way?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-161395@lists.php.net to get a copy of this message
* Thus wrote Daniel Bray (dbray@cfl.rr.com): > 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: I dont see how finding a random table could be a common thing. > > <?php > srand ((float) microtime() * 10000000); You don't need to call srand with php >= 4.2.0. > $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); Instead of looping through all the tables and building an array you can do something like: $n = mysql_num_rows($result); $seek = mt_rand(0, $n-1); mysql_data_seek($result, $seek); Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#161395) next »