PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
satikas
Forum Newbie
Posts: 19 Joined: Sat Mar 28, 2009 9:14 am
Post
by satikas » 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:
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
Probably the "limit" line, but just cant figure out whats wrong with it.
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> ";
}
?>
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sat Aug 22, 2009 1:08 pm
Think about it - how can you limit to a negative number??
I'm not entirely sure what the problem is...I don't really feel like running through your entire script x_x But you probably need to sort your logic out.
Darhazer
DevNet Resident
Posts: 1011 Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria
Post
by Darhazer » Sat Aug 22, 2009 2:02 pm
Offtopic:
Code: Select all
$data = mysql_query("SELECT * FROM notepads WHERE owner='$ename'") or die(mysql_error());
$rows = mysql_num_rows($data);
DO NOT use this to check number of records
Use this instead:
Code: Select all
$data = mysql_query("SELECT COUNT(*) AS NumRecords FROM notepads WHERE owner='$ename'") or die(mysql_error());
$result = mysql_fetch_object($data);
$rows = $result->NumRecords;
And about the problem, try casting pagenum to int:
Code: Select all
$pagenum = intval($_GET["pagenum"]);