note 4237 deleted from rand by hholzgra
| From: | hholzgra@php.net | Date: | Fri, 25 Aug 2000 10:40:11 +0000 |
| Subject: | note 4237 deleted from rand by hholzgra | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-3231@lists.php.net to get a copy of this message | ||
Here's a bit of code I use in an include file to generate a random record from a mySQL database
that has an ID field and one other field. Contact me with questions.
<?php
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
//This code should be included in a file needing a random number function
//Include file for Random Number and SQL Row Generator
//Matthew Jett Hall -- www.newhmw.com -- Tuesday, 7, March, 2000 at 01:04:04
//matthewjetthall@newhmw.com / www.tastylinux.com
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
// $long_seed = the seed for the random number
// $str_tableName = the name of the table upon which to perform the random generation
// =-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=-=-=-=--=-=-=-=-=-=--=-=
function randomNumber($long_seed) {
// seed with microseconds since last "whole" second
srand((double)microtime()*1000000);
$randval = rand(1,$long_seed);
return($randval);
}
function randomItem($str_tableName) {
//get a random item from the table based upon the table count
require('db.inc'); //connect to the database
//get the number of records in the database
$str_SQL='select * from ' . $str_tableName;
$result=mysql_query($str_SQL); //check the # of records
$int_numberOfRows = @mysql_num_rows($result); //count how many records there are
//select a single record based upon a random # generated from the row count
$str_SQL='select * from ' . $str_tableName . ' where ID='.
randomNumber($int_numberOfRows);
$result=mysql_query($str_SQL);
while ($myrow=mysql_fetch_row($result)){
$hold=$myrow[1];
}
return($hold);
}
?>