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
https://forums.phpfreaks.com/topic/220022-pagination-fail-boolean/
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() ); 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.