note 34265 added to function.rand
| From: | webmaster at lgcc-kc dot org | Date: | Sun, 20 Jul 2003 16:46:41 +0000 |
| Subject: | note 34265 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52417@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34265
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34265&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34265&report=yes