469,903 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

pagination error

omerbutt
638 512MB
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.  
Jun 17 '09 #1

✓ answered by omerbutt

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.  

15 1622
Atli
5,058 Expert 4TB
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.
Jun 17 '09 #2
omerbutt
638 512MB
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
Jun 17 '09 #3
omerbutt
638 512MB
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
Jun 17 '09 #4
Atli
5,058 Expert 4TB
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
Jun 17 '09 #5
Atli
5,058 Expert 4TB
@omerbutt
It always returns a resource ID on success, even if it's an empty set.
Jun 17 '09 #6
omerbutt
638 512MB
@Atli
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

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
Jun 18 '09 #7
omerbutt
638 512MB
@Atli
then what should i be comparing $result with ?
Jun 18 '09 #8
Atli
5,058 Expert 4TB
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.
Jun 18 '09 #9
omerbutt
638 512MB
@Atli
no i checked it that way too it is still entring that if() part :(
Jun 18 '09 #10
Atli
5,058 Expert 4TB
@omerbutt
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 :)
Jun 18 '09 #11
omerbutt
638 512MB
@Atli

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
Jun 18 '09 #12
Atli
5,058 Expert 4TB
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.
Jun 18 '09 #13
omerbutt
638 512MB
@Atli
yes it is not returning any thing nor have i added it.
Jun 19 '09 #14
Atli
5,058 Expert 4TB
@omerbutt
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.
Jun 19 '09 #15
omerbutt
638 512MB
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.  
Jun 24 '09 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by leegold2 | last post: by
2 posts views Thread by Chris H | last post: by
11 posts views Thread by ste | last post: by
3 posts views Thread by assgar | last post: by
1 post views Thread by shalini jain | last post: by
1 post views Thread by Alec | last post: by
16 posts views Thread by gnawz | last post: by
2 posts views Thread by kkshansid | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.