Jump to content

it will say pagination and sql query problem


digitalmartyr

Recommended Posts

so whats going on here is that if i comment out the area where the limit is added onto the query the query works and my data gets displayed. if i try to add into the mysql query the limit statement, its goes wrong. what can i do?

 

<?php
require('dbconnect.php');
//
if($_GET['page'])
{
$pagesection = $_GET['page'];
}
if(isset($_GET['cat']))
{
$prodCat = $_GET['cat'];
echo $prodCat;
}
//

if(!(isset($pagenum)))
{
//no page number default to 1
$pagenum = 1;
}
//get sql results and count
//see what cat is called
if($prodCat == 'all')
{
$data = mysql_query("SELECT * FROM products");	
}
else
{
$data = mysql_query("SELECT product_name, product_price, product_dscrb, product_img FROM products WHERE category = '$prodCat'");
}
//
$rows = mysql_num_rows($data);
//number of results per page
$page_rows = 3;

//tells the page number of our last page
$last = ceil($rows/$page_rows);
//make sure page number isnt below 1 or more than maxpages
if($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//sets the range to display in our query
//$max = ' LIMIT '.($pagenum-1) * $page_rows.','.$page_rows;
//$maxquery = $data.$max;
//echo $maxquery;
//run query to display results *style for display* 
//add $max to it for limiting result
//$data = mysql_query($maxquery);

while($info = mysql_fetch_array($data))
{
echo '<div id="productThumb">';
echo '<h3>product id: '.$info['product_id'].'</h3>';
echo '<h2>product name: '.$info['product_name'].'</h2>';
echo '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
echo '<span>Product Describtion '.$info['product_dscrb'].'</span>';
echo '</div>';

}
//shows user what page they are on and the total number of pages
echo "<p>--page $pagenum of $last--<p>";

//check if on page 1, if so we dont need a link to the prev page or first page so do nothing
//if we arent on page 1 we generate links to the first page and to the prev pages
if($pagenum == 1)
{

}
else
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=1'>
<<-First</a>";
echo " ";
$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$previous'>
<-Previous</a>";
}

//this does the same above, only checking to see if we are on the last page
//and then generating the NExt and Last link
if($pagenum == $last)
{

}
else
{
$next = $pagenum+1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$next'>Next-></a>";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$last'>Last->></a>";
}
?>

Link to comment
Share on other sites

You are setting your $data variable equal to the mysql function mysql_query(), which will return a resource handle to the database, not a string.  When you try to concatenate your LIMIT clause onto your query you are attempting to add it onto the resource handle and it is returning an error.  In your first if/else statement that sets the value of $data depending on the value of $prodCat, change it to this:

 

<?php

if($prodCat == 'all')
{
$data = "SELECT * FROM products";	
}
else
{
$data = "SELECT product_name, product_price, product_dscrb, product_img FROM products WHERE category = '$prodCat'";
}

?>

 

That way you are appending your LIMIT clause onto the end of the $data string, and not the $data resource, so when you use it later in the mysql_query() function it is valid SQL syntax.

 

Theo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.