SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: Random function problem
-
Apr 28, 2005, 11:43 #1
- Join Date
- Feb 2005
- Location
- Venezuela
- Posts
- 144
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Random function problem
hello,
im having trouble with the random function on a page, its selecting only the first register of the select
PHP Code:$rsmotd = mysql_query("select count(idmotd) motdct from motd where idmotd in (16,17,18)", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
// Making the Random process using the count used before
$radnmod = rand(1,$rowsmotd["motdct"]);
// Selecting the Random MOTD
$rsmotd = mysql_query("select * from motd where idmotd in (16,17,18)", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
please, little help over here.....!!!!...run Forrest....run!!
-
Apr 28, 2005, 12:28 #2
- Join Date
- Jan 2001
- Location
- England
- Posts
- 338
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
mysql_fetch_array() fetches the first row of the result set and moves the pointer to the next row, a subsequent call to mysql_fetch_array() will return the second row and so forth. Because you only ever call it once you will only ever get the first row of the result.
I can't see where you use the variable $radnmod in this code slice, which you get from the rand() funtion. NOTE: that in php versions prior to 4.2.0 you need to seed the rand() function with srand() before calling rand() otherwise you will get the same random number sequence each time.
For your code to work you would need to return the result set into an array and pick the index using your random number.
PHP Code:// your code here
while($aRow = mysql_fetch_array($rsmotd)){
$aResult[] = $aRow;
}
$aRandomRow = $aResult[$radnmod];
PHP Code:$rsmotd = mysql_query("select count(idmotd) motdct from motd where idmotd in (16,17,18)", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
// Making the Random process using the count used before
//$radnmod = rand(1,$rowsmotd["motdct"]);
$radnmod = rand(0,$rowsmotd["motdct"]-1);
// Selecting the Random MOTD
// $rsmotd = mysql_query("select * from motd where idmotd in (16,17,18)", $conn);
// $rowsmotd = mysql_fetch_array($rsmotd);
// Pick just one row from the table
$rsmotd = mysql_query("select * from motd where idmotd in (16,17,18) LIMIT $radnmod,1", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
Drinky
-
Apr 28, 2005, 13:43 #3
- Join Date
- Feb 2005
- Location
- Venezuela
- Posts
- 144
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry man, it doesnt work
and it gives me this error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/httpd/html/index.php on line 17
this is line 17
PHP Code:$rowsmotd = mysql_fetch_array($rsmotd);
then i added 3 registers this morning, and i want to just select randomly these 3 new registers, this is the code i used for the 15 register table, it works ok
PHP Code:$rsmotd = mysql_query("select count(idmotd) motdct from motd ", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
// Making the Random process using the count used before
$radnmod = rand(1,$rowsmotd["motdct"]);
// Selecting the Random MOTD
$rsmotd = mysql_query("select * from motd where idmotd='$radnmod'", $conn);
$rowsmotd = mysql_fetch_array($rsmotd);
...run Forrest....run!!
Bookmarks