Pagination mysql error
Posted: Sat Aug 22, 2009 11:22 am
This script seems to work fine when there are records to show. However when the table is empty it gives:
Probably the "limit" line, but just cant figure out whats wrong with it.
Code: Select all
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: Select all
<?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> ";
}
?>