Web Hosting Talk







View Full Version : MySQL Error - Unable to jump to row


latheesan
06-23-2006, 10:05 PM
Hello. I'm working on a new script and found my self stuck on a new hurdle. Hope you guys help me out once again.

I am working on pagination for my script and this is how the coding looks so far:

<?php

/* Connect To MySQL Server */
mysql_connect($host, $user, $pass) or trigger_error("SQL", E_USER_ERROR);
mysql_select_db($db) or trigger_error("SQL", E_USER_ERROR);

/* Obtain The Required Page Number */
if(isset($_GET["N"]))
{
$pageno = $_GET["N"];
} else {
$pageno = 1;
}

/* Identify How Many Rows Are Available In The Database */
$query = "SELECT count(*) FROM users WHERE active = '1'";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

/* Calculate Number of $lastpage */
$rows_per_page = 5;
$lastpage = ceil($numrows/$rows_per_page);

/* Ensure Pageno is Within Range */
$pageno = (int)$pageno;
if($pageno < 1)
{
$pageno = 1;
}
else
if($pageno > $lastpage)
{
$pageno = $lastpage;
}

/* Construct LIMIT Clause */
$limit = 'LIMIT ' . ($pageno - 1) * $rows_per_page . ',' . $rows_per_page;

/* Query Database Collect Data */
$query = "SELECT * FROM users WHERE active = '1' $limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
mysql_close();

/* Display Data */
$i = 0;
while($i < $rows_per_page)
{
/* Construct Data */
$myspace_id = mysql_result($result, $i, "myspace_id");
$name = mysql_result($result, $i, "name");
$age = mysql_result($result, $i, "age");
$category = mysql_result($result, $i, "category");
$notes = mysql_result($result, $i, "notes");
$photo = mysql_result($result, $i, "photo");

/* Print Data Here */

/* Run Loop */
$i++;
}

?>

I had 13 records in the db, so Page 1, 2 worked fine. When i clicked on page 3, i started to get this error:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 3 on MySQL result index 10 in C:\WWW\xampp\htdocs\html\Home.php on line 78

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 3 on MySQL result index 10 in C:\WWW\xampp\htdocs\html\Home.php on line 79

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 3 on MySQL result index 10 in C:\WWW\xampp\htdocs\html\Home.php on line 80

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 3 on MySQL result index 10 in C:\WWW\xampp\htdocs\html\Home.php on line 81

etc...

I have a slight idea why this error is caused. I am thinking this error is shown because there isnt enough records in the data to fulfil the LIMIT construct on page 3. If thats the case, then, how can you solve such a problem? I mean, get the script to only construct the last page for only the amount of records left...

Thanks in advance for your input guys :)

tiamak
06-24-2006, 12:43 AM
I have a slight idea why this error is caused. I am thinking this error is shown because there isnt enough records in the data to fulfil the LIMIT construct on page 3. If thats the case, then, how can you solve such a problem? I mean, get the script to only construct the last page for only the amount of records left...

Thanks in advance for your input guys :)

right :) thats ur problem

to fix that simply use mysql_fetch_array in ur while loop :) (or any other fetch function)

or if u really want that mysql_result then ..... add @ before function :D but that would be quite lame :D

latheesan
06-24-2006, 06:07 AM
Great suggestion tiamak about using mysql_fetch_array(); within the while loop.

The error seems to have gone now, how ever, blank records are still being printed when there isnt enough records to print based on $rows_per_page requirement.

This sample code:

<?php

/* Connect To DB */
mysql_connect("localhost", "root", "lathee1987") or die(mysql_error());
mysql_select_db("sms");

/* Obtain The Required Page Number */
if(isset($_GET["pageno"]))
{
$pageno = $_GET["pageno"];
}
else
{
$pageno = 1;
}

/* Identify How Many Rows Are Available In The Database */
$query = "SELECT count(*) FROM users";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

/* Calculate Number of $lastpage */
$rows_per_page = 4;
$lastpage = ceil($numrows/$rows_per_page);

/* Ensure Pageno is Within Range */
$pageno = (int)$pageno;
if($pageno < 1)
{
$pageno = 1;
}
else
if($pageno > $lastpage)
{
$pageno = $lastpage;
}

/* Construct LIMIT Clause */
$limit = 'LIMIT ' . ($pageno - 1) * $rows_per_page . ',' . $rows_per_page;

/* Query Database Collect Data */
$query = "SELECT * FROM users $limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
mysql_close();

/* Display Data */
$i = 0;
while($i < $rows_per_page)
{
/* Construct Data */
$arr = mysql_fetch_array($result);
$id = $arr["user_id"];
$username = $arr["username"];
$password = $arr["password"];
$email = $arr["email"];

/* Print Data */
echo "ID - " . $id . "<br>";
echo "Username - " . $username . "<br>";
echo "Password - " . $password . "<br>";
echo "Email - " . $email . "<br><br>";

/* Run Loop */
$i++;
}

/* Print Pagination Hyperlinks */
if($pageno == 1)
{
echo " First Prev ";
}
else
{
$prevpage = $pageno - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>First</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>Prev</a> ";
}

echo " ( Page $pageno of $lastpage ) ";

if($pageno == $lastpage)
{
echo " Next Last ";
}
else
{
$nextpage = $pageno + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>Next</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>Last</a> ";
}

?>

Outputs:

http://img77.imageshack.us/img77/4800/untitled7gd1.png

Are there any possible fix for this?

latheesan
06-24-2006, 06:15 AM
Never mind, i found a great fix. All i had to do was, workout how many rows were returned for the query with the LIMIT construct in $num var, then put that into the while loop, instead of $rows_per_page var. Yeyy :banana:

This is the small fix:

/* Query Database Collect Data */
$query = "SELECT * FROM users $limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$num = mysql_num_rows($result);
mysql_close();

/* Display Data */
$i = 0;
while($i < $num)
{
/* Construct Data */
$arr = mysql_fetch_array($result);
$id = $arr["user_id"];
$username = $arr["username"];
$password = $arr["password"];
$email = $arr["email"];

/* Print Data */
echo "ID - " . $id . "<br>";
echo "Username - " . $username . "<br>";
echo "Password - " . $password . "<br>";
echo "Email - " . $email . "<br><br>";

/* Run Loop */
$i++;
}