Page 1 of 1

Unable to jump to row 8 on MySQL result index 4

Posted: Wed Nov 05, 2003 9:02 am
by kendall
Hello,

I am using PHP's rand function to randomyly select information fomr a mysql database

the following is my code

Code: Select all

$query = "SELECT * FROM banner_images WHERE type = 'B'";
$IMG = mysql_query($query, $Connection);
srand((double)microtime()*1000000);
$random_id = rand(0,mysql_num_rows($IMG));
// display chosen banner
// get random image
$file = mysql_result($IMG,$random_id,'file');
$id = mysql_result($IMG,$random_id,'ID');
$displays = mysql_result($IMG,$random_id,'displays') + 1;
on running the code it works but sometimes i get the following error

Code: Select all

Warning: mysql_result(): Unable to jump to row 8 on MySQL result index 4  on line 33
what does that mean really?
Kendall

Posted: Wed Nov 05, 2003 9:40 am
by JAM
mysql_num_rows() resturns an int starting at 1 (5 rows = 5)
mysql_result() starts fetching at 0 (row 5 = 4)

Or so I think... ;)

Consider:
mysql_fetch_row(), faster when fetching more than one cell (ok, so you don't likely will notice any difference but... "I can make it fast, so i should!" ;))

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically... (php.net)

Posted: Wed Nov 05, 2003 10:44 am
by Weirdan
You can simplify your code using rand() sql function:

Code: Select all

SELECT * FROM banner_images WHERE type = 'B' order by rand() limit 1
It's more efficient to select one row from a database. And SQL may randomize result for you :)

Posted: Wed Nov 05, 2003 10:55 am
by kendall
Wierdan,

Thanks man i didnt know you cud have done that