Connecting Tech Pros Worldwide Forums | Help | Site Map

pagination error

omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#1: Jun 17 '09
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
Expand|Select|Wrap|Line Numbers
  1. $total = mysql_result($result, 0, 0) or die(mysql_error());
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. class Pager{
  2.        function getPagerData($numHits, $limit, $page){ 
  3.            $numHits  = (int) $numHits; 
  4.            $limit    = max((int) $limit, 1); 
  5.            $page     = (int) $page; 
  6.            $numPages = ceil($numHits / $limit);
  7.            $page = max($page, 1); 
  8.            $page = min($page, $numPages);
  9.            $offset = ($page - 1) * $limit;
  10.            $ret = new stdClass;
  11.            $ret->offset   = $offset; 
  12.            $ret->limit    = $limit; 
  13.            $ret->numPages = $numPages; 
  14.            $ret->page     = $page; 
  15.            return $ret; 
  16.        } 
  17.        }
  18.     if(!empty($_GET['bingo'])){$cat_id=$_GET['bingo'];}else{$cat_id='24';}
  19.     if(!empty($_REQUEST['page'])){$page = $_REQUEST['page'];}else{$page=1;}
  20.     $limit=6;
  21.     $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");
  22.  
  23.     $total = mysql_result($result, 0, 0) or die(mysql_error());
  24.     $pager = Pager::getPagerData($total, $limit, $page);
  25.     $offset= $pager->offset;
  26.     $limit= $pager->limit;
  27.     $page= $pager->page;
  28.     $error="";
  29.  

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jun 17 '09

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.
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#3: Jun 17 '09

re: pagination error


i added this statement
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
  2.         $total = mysql_result($result, 0, 0) or die(mysql_error());
  3.     }else{
  4. $total = 0;
  5. $error="No results matched your query";
  6. }
  7.  
but it still not working it still does not pass that line
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#4: Jun 17 '09

re: pagination error


also that when i click any category which does not have any product listed against it the line
Expand|Select|Wrap|Line Numbers
  1. $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");
  2.  
this line does not return false to $result infact it returns Resource id #5
regards,
Omer Aslam
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#5: Jun 17 '09

re: pagination error


And you just get an empty page... odd.

Have you tried turning the error messages on?
Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
  2. error_reporting(E_ALL);

Also:
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
!=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
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#6: Jun 17 '09

re: pagination error


Quote:

Originally Posted by omerbutt View Post

also that when i click any category which does not have any product listed against it the line

Expand|Select|Wrap|Line Numbers
  1. $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");
  2.  
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.
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#7: Jun 18 '09

re: pagination error


Quote:

Originally Posted by Atli View Post

And you just get an empty page... odd.

Have you tried turning the error messages on?

Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
  2. error_reporting(E_ALL);

YES i had already turned
error_reporting(E_ALL)
also i included
Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
but there were no errors displayed

Quote:
Also:
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
!=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
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#8: Jun 18 '09

re: pagination error


Quote:

Originally Posted by Atli View Post

It always returns a resource ID on success, even if it's an empty set.

then what should i be comparing $result with ?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#9: Jun 18 '09

re: pagination error


Have you tried counting the rows?
Expand|Select|Wrap|Line Numbers
  1. if($result && mysql_num_rows($result) > 0) {
  2.     mysql_do_whatever_you_want($result);
  3. }
  4. else {
  5.     echo "Sorry. No results.";
  6. }
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.
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#10: Jun 18 '09

re: pagination error


Quote:

Originally Posted by Atli View Post

Have you tried counting the rows?

Expand|Select|Wrap|Line Numbers
  1. if($result && mysql_num_rows($result) > 0) {
  2.     mysql_do_whatever_you_want($result);
  3. }
  4. else {
  5.     echo "Sorry. No results.";
  6. }
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 :(
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#11: Jun 18 '09

re: pagination error


Quote:

Originally Posted by omerbutt View Post

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.
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) FROM mytbl";
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $count = mysql_result($result, 0, 0);
  4.  
  5. if($count > 0) {
  6.     // Do your thing here.
  7. }
  8. else {
  9.     echo "Sorry. No cookie for you.";
  10. }
Just make sure the code inside the if block prints something :)
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#12: Jun 18 '09

re: pagination error


Quote:

Originally Posted by Atli View Post

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.

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) FROM mytbl";
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $count = mysql_result($result, 0, 0);
  4.  
  5. if($count > 0) {
  6.     // Do your thing here.
  7. }
  8. else {
  9.     echo "Sorry. No cookie for you.";
  10. }
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
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#13: Jun 18 '09

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.
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#14: Jun 19 '09

re: pagination error


Quote:

Originally Posted by Atli View Post

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.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#15: Jun 19 '09

re: pagination error


Quote:

Originally Posted by omerbutt View Post

yes it is not returning any thing nor have i added it.

What do you mean?

In the code you posted, you do this:
Expand|Select|Wrap|Line Numbers
  1. $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.
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 344
#16: Jun 24 '09

re: pagination error


i did it here see the example here
http://toylandpk.com/
here is what i did
Expand|Select|Wrap|Line Numbers
  1. $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");
  2.     //$result = mysql_query("select count(*) from ".PRODUCT." where ".PRODUCT.".prod_status='1'");
  3.     if(mysql_num_rows($result)){
  4.         $query_data=mysql_fetch_array($result);
  5.         $total=$query_data[0];
  6.     }
  7.     if($total>0){
  8.         $pager = Pager::getPagerData($total, $limit, $page);
  9.         $offset= $pager->offset;
  10.         $limit= $pager->limit;
  11.         $page= $pager->page;
  12.     }
  13.  
Reply