0

Hi, I have tried to use pagination for my website that I'm creating, when I click on next page or last page it gives me this problem:

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 'ORDER BY Class, Title limit 1, 30' at line 1

With that I can't see the page properly and I don't know what went wrong or how to amend it

The code is

    <?php
// Connects to your Database
require_once('ConnectionSQL.php');
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
    $pagenum = 1;
}
//Here we count the number of results

//Edit $data to be your query
if ($Classification == "all") {
    $whereClause = "";
} else {
    $whereClause = "WHERE truncate(s.Class,1)=$Classification";
}

$data = mysql_query("SELECT Work_id, Author, Title, s.Class FROM l_stock s $whereClause ORDER BY Class, Title") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 30;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
    $pagenum = 1;
}
elseif ($pagenum > $last)
{
    $pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
if ($Classification == "all") {
    $whereClause = "";
} else {
    $whereClause = "WHERE truncate(s.Class,1)=$Classification";
}

$data_p = mysql_query("SELECT Work_id, Author, Title, s.Class FROM l_stock s $whereClause ORDER BY Class, Title $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
    Print $info['Name'];
}
// This shows the user what page they are on, and the total number of pages
echo "<p>Page $pagenum of $last";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{ }
else
{
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a></p>";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
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> ";
}
?>

Thank you in advance!

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by simplypixie
0

This is wrong

if (!(isset($pagenum)))

Should be

if (!isset($pagenum))

You are missing the OFFSET in your query, your query should be in this format:

mysql_query("SELECT table_columns FROM table_name WHERE conditional ORDER BY column_name ASC_or_DESC LIMIT num_rows_per_page OFFSET curr_page_number_*_num_rows_per_page");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.