Unable to jump to row 8 on MySQL result index 4

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Unable to jump to row 8 on MySQL result index 4

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Wierdan,

Thanks man i didnt know you cud have done that
Post Reply