note 34265 deleted from function.rand by didou
| From: | didou@php.net | Date: | Tue, 29 Jul 2003 10:50:20 +0000 |
| Subject: | note 34265 deleted from function.rand by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-52892@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster@lgcc-kc.org
----
I had problems with RAND () in the Select statement for mysql. It seems that the database generated
the same result when I limited it to 1. this was the statement:
$bannerQuery= "Select imagepath, website FROM resources Where imagepath!='' AND
wesbite!='';";
This did not work as planned..it generated the same banner ad each time. So, I played around with
my scripts, and decided to load an array beforehand and then select a random id from that, like so:
$bannerQuery= "Select id FROM resources Where website != '' AND imagepath !=
'';";
$bannerExec= mysql_query($bannerQuery);
$n = mysql_num_rows($bannerExec);
$max = mysql_num_rows($bannerExec);
while ($row = mysql_fetch_array($bannerExec)) {
$num[$n]="$row[id]";
$n=$n-1;
}
$rand_num = rand(1,$max);
$bannerQuery= "Select imagepath, website FROM resources Where id =
'$num[$rand_num]';";
A Quick note, this new way requires that the rand start with 1 as the while script causes a value of
$num[0]...there are no ids with values of 0. Hope this helps someone!