I tried to search the forums but couldn't find same issue.

I am running a script to find the "Previous" and "Next" records, the problem is when I get to record 1 I get the error below, same with when I reach the last record in the db.

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 18 

Here is the script running it, do I need to add to the query? or how can I use IF/ELSE?

<?php 
$id = $_GET['id']; 
// Retrieve your record according to id 
// Display Record 

// Links to Previous Records
$query = "SELECT id FROM jokes WHERE category = '$category' AND id < $id ORDER BY id DESC LIMIT 1" ; // For Previous 
$result = mysql_query($query) ;
$rowp = mysql_result($result,0); // $rowp has Previous record id 
$query2 = "SELECT id FROM records WHERE id > $id ORDER BY ASC LIMIT 1"; // For next Record 


// Links to Next Records
$query = "SELECT id FROM jokes WHERE category = '$category' AND id > $id ORDER BY id ASC LIMIT 1" ; // For Next 
$result = mysql_query($query) ;
$rown = mysql_result($result,0); // $rown has Next record id 
$query2 = "SELECT id FROM records WHERE id < $id ORDER BY ASC LIMIT 1"; // For previous Record 
?>


<center>
<a href="jokecontent.php?id=<? echo "$rowp"; ?>">Previous Joke</a>

&nbsp;&nbsp;&nbsp;&nbsp;


<a href="jokecontent.php?id=<? echo "$rown"; ?>">Next Joke</a> 
</center>

    Sounds like your query didn't return any rows. Might want to do some SQL debugging here.

    When this script is used in production evironments, you might want to use something like [man]mysql_num_rows/man to check that the resulting query actually contains rows before you try to access them.

      bradgrafelman -

      Thanks for the reply, the script works fine until it hits either the first or last record in the database, all other work fine.

      I just don't know how to stop at the last record, it keeps trying to find more.

      Here is a link to a page that works fine but look half way down center column to the "Previous" and "Next" links and try to move to the ends and you will see what I mean.

      http://www.blondesandrednecks.com/jokecontent.php?id=109

        all you need to do is check to see if a row exists for each one, and if not, don't display the link.. can be done using mysql_num_rows

        
        $query = "SELECT field FROM table";
        $result = mysql_query($query);
        $numrows = mysql_num_rows($result);
        $rowp = mysql_result($result,0);
        $query2 = "SELECT field FROM table";
        
        $query = "SELECT field FROM table";
        $result = mysql_query($query); 
        $numrows2 = mysql_num_rows($result);
        $rown = mysql_result($result,0);
        $query2 = "SELECT field FROM table";
        
        echo "<center>";
        
        if(empty($numrows)) { // no rows returned
        
        // what to do if no rows returned
        
        }else{
        
        ?>
        
        <a href="jokecontent.php?id=<? echo "$rowp"; ?>">Previous Joke</a> 
        
        <?php
        
        }
        
        if(empty($numrows2)) { // no rows returned again
        
        // what  to do if no rows returned here
        
        }else{
        
        ?>
        
        <a href="jokecontent.php?id=<? echo "$rown"; ?>">Next Joke</a> 
        
        <?php
        
        }
        echo "</center>";
        
        ?>
        

          notaloser -

          Thanks for putting that together for me.

          I did a little modifying to suit my needs and it works exactly like I wanted.

          Below is the final script for anyone that may want to see.

          $query = "SELECT id FROM jokes WHERE category = '$category' AND id < $id ORDER BY id DESC LIMIT 1" ; // For Previous 
          $result = mysql_query($query); 
          $numrows = @mysql_num_rows($result); 
          $rowp = @mysql_result($result,0); 
          $query2 = "SELECT id FROM records WHERE id > $id ORDER BY ASC LIMIT 1"; // For next Record
          
          $query = "SELECT id FROM jokes WHERE category = '$category' AND id > $id ORDER BY id ASC LIMIT 1" ; // For Previous 
          $result = mysql_query($query); 
          $numrows2 = @mysql_num_rows($result); 
          $rown = @mysql_result($result,0); 
          $query2 = "SELECT id FROM records WHERE id < $id ORDER BY ASC LIMIT 1"; // For next Record 
          
          echo "<center>"; 
          
          if(empty($numrows)) { // no rows returned 
          
          // what to do if no rows returned 
          
          }else{ 
          
          ?> 
          
          <a href="jokecontent.php?id=<? echo "$rowp"; ?>"><b>Previous Joke</b></a>&nbsp;&nbsp;&nbsp;&nbsp;
          
          <?php 
          
          } 
          
          if(empty($numrows2)) { // no rows returned again 
          
          // what  to do if no rows returned here 
          
          }else{ 
          
          ?> 
          
          <a href="jokecontent.php?id=<? echo "$rown"; ?>"><b>Next Joke</b></a> 
          
          <?php 
          
          } 
          echo "</center>"; 
          
          ?>
            Write a Reply...