Re: PEAR/DB: Random row from result set

From: Date: Tue, 24 Apr 2001 18:16:39 +0000
Subject: Re: PEAR/DB: Random row from result set
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-90@lists.php.net to get a copy of this message
Sebastian Bergmann wrote: > > Hi there, > > I'm currently wondering if there's a more elegant way to do the > following: > > $rows = $db_result->numRows(); > > switch ($rows) { > case 0: { > die("..."); > } > break; > > case 1: { > $row = $db_result->fetchRow(); > } > break; > > default: { > mt_srand((double) microtime() * 1000000); > $random_index = mt_rand(1, $rows); > > for ($i = 0; $i < $random_index; $i++) { > $row = $db_result->fetchRow(); > } > } > break; > } > > I just need a random row from a result set :-/ I recently added the "fetch arbitrary rows" ability for extensions. To use it, simply do: $row = $db_result->fetchRow($fetchmode, $row_number_to_fetch); This feature is unsupported in oci8 and ibase :-( Tomas V.V.Cox

« previous php.pear.general (#90) next »