Jump to content

Pagination fail: Boolean?!


mcfmullen

Recommended Posts

So this is the pagination code I am trying to use:

<?php
include("variables.php");

if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

$data = mysql_query("SELECT * FROM $itemspec");

$rows = mysql_num_rows($data); 
$page_rows = 4;
$last = ceil($rows/$page_rows); 

if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

$data_p = mysql_query("SELECT * FROM $itemspec .$max");
?>

<?php
while($row = mysql_fetch_array( $data_p )){
// output of query//
}

 

While in theory it should work, I get this error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\items.php on line 115

 

With line 115 being

while($row = mysql_fetch_array( $data_p )){

 

However, If i edit

$data_p = mysql_query("SELECT * FROM $itemspec .$max");

 

to:

$data_p = mysql_query("SELECT * FROM $itemspec LIMIT 0,10");

 

the code works fine.

 

Can anyone figure out what my problem is because nothing I do seems to work!

 

Link to comment
Share on other sites

Your query execution is failing and returning a boolean false. Separate the query string from the query execution, and echo it along with any errors returned by mysql_error()

$query = "SELECT whatever";
$data_p = mysql_query($query) or die( "<br>Query string $query<br>Returned errors: " . mysql_error() ); 

Link to comment
Share on other sites

Very good. Just be aware that sending the query string and errors to the screen is for development, and on a live, production site, they should be logged instead, with a generic error message sent to the screen like "Sorry, there was a database problem" so as not to give up too much information.

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.