note 15823 added to function.rand
| From: | tobbe dot m at home dot se | Date: | Thu, 04 Oct 2001 08:03:36 +0000 |
| Subject: | note 15823 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-18553@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>";
--
http://www.php.net/manual/en/function.rand.php
http://master.php.net/manage/user-notes.php?action=edit+15823
http://master.php.net/manage/user-notes.php?action=delete+15823
http://master.php.net/manage/user-notes.php?action=reject+15823