note 15823 deleted from function.rand by jimw

From: Date: Tue, 18 Dec 2001 19:31:23 +0000
Subject: note 15823 deleted from function.rand by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-23151@lists.php.net to get a copy of this message
# Simple code to get a random row from a table with mysql < 3.23 # (in 3.23 you can use SELECT * FROM $table ORDER BY RAND() ) $i=0; $table="quote_tbl"; mysql_connect("$DBHost","$DBUser","$DBPass"); mysql_select_db("$DB"); $result=mysql_db_query($DB, "SELECT quote,author FROM $table"); $num_quotes=mysql_num_rows($result); $this_quote=rand(1,$num_quotes); while ($myrow = mysql_fetch_array($result)) { $quote=$myrow[quote]; $author=$myrow[author]; $i++; if($i == $this_quote) { break; } } echo "<h3>Today's quote</h3>\"<i>$quote</i>\"<br><b>$author</b><br>";

« previous php.notes (#23151) next »