pagination error  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| |
hi i have made a simple pagination but it is creating problem , the problem is that if there are any products under the category that is clicked in the left menu then the page works fine but if it does not have any product listed under that category then the page halts no eror is diplayed and the page halts at the line -
$total = mysql_result($result, 0, 0) or die(mysql_error());
-
the working example can be seen here link
here is the full code for that pagination each time the page loads it checks if the variable $bingo is poted or not which contains a cat_id and against that category id it fetches the results and makes the pages -
class Pager{
-
function getPagerData($numHits, $limit, $page){
-
$numHits = (int) $numHits;
-
$limit = max((int) $limit, 1);
-
$page = (int) $page;
-
$numPages = ceil($numHits / $limit);
-
$page = max($page, 1);
-
$page = min($page, $numPages);
-
$offset = ($page - 1) * $limit;
-
$ret = new stdClass;
-
$ret->offset = $offset;
-
$ret->limit = $limit;
-
$ret->numPages = $numPages;
-
$ret->page = $page;
-
return $ret;
-
}
-
}
-
if(!empty($_GET['bingo'])){$cat_id=$_GET['bingo'];}else{$cat_id='24';}
-
if(!empty($_REQUEST['page'])){$page = $_REQUEST['page'];}else{$page=1;}
-
$limit=6;
-
$result = mysql_query("SELECT count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
-
-
$total = mysql_result($result, 0, 0) or die(mysql_error());
-
$pager = Pager::getPagerData($total, $limit, $page);
-
$offset= $pager->offset;
-
$limit= $pager->limit;
-
$page= $pager->page;
-
$error="";
-
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error
Hi.
You don't actually check that the query was successful before you use the mysql_result function.
You should always check the result object from a mysql_query call, or any other function that returns a result, before you use it.
Try adding a if statement that simply checks if the $result variable is false, and print an error if it is.
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error
i added this statement -
if($result!=false){
-
$total = mysql_result($result, 0, 0) or die(mysql_error());
-
}else{
-
$total = 0;
-
$error="No results matched your query";
-
}
-
but it still not working it still does not pass that line
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error
also that when i click any category which does not have any product listed against it the line -
$result= mysql_query("select count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
-
this line does not return false to $result infact it returns Resource id #5
regards,
Omer Aslam
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error
And you just get an empty page... odd.
Have you tried turning the error messages on? - ini_set('display_errors', true);
-
error_reporting(E_ALL);
Also: !=false is a double-negative. You can remove it and get the same results.
Unless you need to check against the actual boolean value FALSE, in which case you should do !==false |  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error Quote:
Originally Posted by omerbutt also that when i click any category which does not have any product listed against it the line -
$result= mysql_query("select count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
-
this line does not return false to $result infact it returns Resource id #5
regards,
Omer Aslam It always returns a resource ID on success, even if it's an empty set.
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error Quote:
Originally Posted by Atli And you just get an empty page... odd.
Have you tried turning the error messages on? - ini_set('display_errors', true);
-
error_reporting(E_ALL);
YES i had already turned error_reporting(E_ALL)
also i included - ini_set('display_errors', true);
but there were no errors displayed Quote:
Also: !=false is a double-negative. You can remove it and get the same results.
Unless you need to check against the actual boolean value FALSE, in which case you should do !==false i didi this way but it is again entring the if part not the else and then it halts as usual
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error Quote:
Originally Posted by Atli It always returns a resource ID on success, even if it's an empty set. then what should i be comparing $result with ?
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error
Have you tried counting the rows? -
if($result && mysql_num_rows($result) > 0) {
-
mysql_do_whatever_you_want($result);
-
}
-
else {
-
echo "Sorry. No results.";
-
}
Like I say, even if there are no results in the set, $result will still return true.
If you want to know if there were any results just testing the $result won't do.
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error Quote:
Originally Posted by Atli Have you tried counting the rows? -
if($result && mysql_num_rows($result) > 0) {
-
mysql_do_whatever_you_want($result);
-
}
-
else {
-
echo "Sorry. No results.";
-
}
Like I say, even if there are no results in the set, $result will still return true.
If you want to know if there were any results just testing the $result won't do. no i checked it that way too it is still entring that if() part :(
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error Quote:
Originally Posted by omerbutt no i checked it that way too it is still entring that if() part :( Gahh. I just realised that you are using a COUNT query.
Those always return a row. (I should have noticed earlier, but I was distracted :P)
You need to fetch the value in that row and test that. -
$sql = "SELECT COUNT(*) FROM mytbl";
-
$result = mysql_query($sql) or die(mysql_error());
-
$count = mysql_result($result, 0, 0);
-
-
if($count > 0) {
-
// Do your thing here.
-
}
-
else {
-
echo "Sorry. No cookie for you.";
-
}
Just make sure the code inside the if block prints something :)
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error Quote:
Originally Posted by Atli Gahh. I just realised that you are using a COUNT query.
Those always return a row. (I should have noticed earlier, but I was distracted :P)
You need to fetch the value in that row and test that. -
$sql = "SELECT COUNT(*) FROM mytbl";
-
$result = mysql_query($sql) or die(mysql_error());
-
$count = mysql_result($result, 0, 0);
-
-
if($count > 0) {
-
// Do your thing here.
-
}
-
else {
-
echo "Sorry. No cookie for you.";
-
}
Just make sure the code inside the if block prints something :)
yuo are not getting what i am trying to tell you the line mysql_result will cause every thing to halt if there is no record and i need to put it into the if check this wont work
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error
Hmmm...
Try to remove the die statement from your mysql_result call.
I'm not sure the mysql_error function would return anything if the mysql_result function returned false, which would halt the script without any explanation.
O, and P.S.
Your Pager::getPagerData function should be declared static if you intend to use it like that.
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error Quote:
Originally Posted by Atli Hmmm...
Try to remove the die statement from your mysql_result call.
I'm not sure the mysql_error function would return anything if the mysql_result function returned false, which would halt the script without any explanation.
O, and P.S.
Your Pager::getPagerData function should be declared static if you intend to use it like that. yes it is not returning any thing nor have i added it.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: pagination error Quote:
Originally Posted by omerbutt yes it is not returning any thing nor have i added it. What do you mean?
In the code you posted, you do this: -
$total = mysql_result($result, 0, 0) or die(mysql_error());
If the result set is empty (no first row to jump to) or the contents of the first field in the first row would evaluate as FALSE (0, null, etc..), then the die function would halt the execution of the code without any warning.
(The mysql_error function doesn't return an error if the mysql_result function fails, thus you get no error message.)
To see what I mean, try simply adding a custom error message in the die, rather than mysql_error.
|  | Needs Regular Fix | | Join Date: Nov 2006 Location: Earth Obviously :P
Posts: 344
| | | re: pagination error
i did it here see the example here http://toylandpk.com/
here is what i did -
$result = mysql_query("SELECT count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
-
//$result = mysql_query("select count(*) from ".PRODUCT." where ".PRODUCT.".prod_status='1'");
-
if(mysql_num_rows($result)){
-
$query_data=mysql_fetch_array($result);
-
$total=$query_data[0];
-
}
-
if($total>0){
-
$pager = Pager::getPagerData($total, $limit, $page);
-
$offset= $pager->offset;
-
$limit= $pager->limit;
-
$page= $pager->page;
-
}
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|