Pagination mysql error

Discussion in 'PHP' started by sittsait, Aug 22, 2009.

  1. #1
    This script seems to work fine when there are records to show. However when the table is empty it gives:
    
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-6,6' at line 1
    
    Code (markup):
    Probably the "limit" line, but just cant figure out whats wrong with it.

    
    <?php 
    
    $pagenum = $_GET["pagenum"];
    $ename = $_COOKIE["ename"];
    
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    
    $data = mysql_query("SELECT * FROM notepads WHERE owner='$ename'") or die(mysql_error());
    $rows = mysql_num_rows($data);
    
    $page_items = 6;
    
    $last = ceil($rows/$page_items);
    
    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }
    
    $max = 'limit ' .($pagenum - 1) * $page_items .',' .$page_items;
    if ($max < 0){ echo $max; die(); }
    $data_p = mysql_query("SELECT * FROM notepads $max") or die(mysql_error());
    
    echo "<center>";
    while($info = mysql_fetch_array( $data_p ))
    {
    echo "<div id='leftcol' style='float:left; width:33%;'>";
    echo $info['name'];
    echo "</div>";
    }
    echo "</center>";
    echo "<p>";
    
    echo " --Page $pagenum of $last-- <p>";
    
    if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    
    echo " ---- ";
    
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    
    Code (markup):
     
    sittsait, Aug 22, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $start = ($pagenum-1) * $page_items;
    
    if ($start < 0) { 
        $start = 0;
    }
    
    $data_p = mysql_query("SELECT * FROM notepads LIMIT " . $start . ", " . $page_items) or die(mysql_error());
    PHP:
     
    premiumscripts, Aug 22, 2009 IP
  3. sittsait

    sittsait Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I appreciate the time you spent helping me out.
    Thanks a bunch.
     
    sittsait, Aug 22, 2009 IP