pagination error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    pagination error

    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
    Code:
    $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
    [code=php]
    class Pager{
    function getPagerData($n umHits, $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=$_GE T['bingo'];}else{$cat_id= '24';}
    if(!empty($_REQ UEST['page'])){$page = $_REQUEST['page'];}else{$page=1; }
    $limit=6;
    $result = mysql_query("SE LECT count(*) FROM ".PRODUCT." p,".PRODUCT_DES CRIPTION." pd,".PRODUCT_TO _CATEGORY." ptc WHERE p.prod_id=pd.pr od_id AND ptc.cat_id='$ca t_id' AND ptc.prod_id=p.p rod_id");

    $total = mysql_result($r esult, 0, 0) or die(mysql_error ());
    $pager = Pager::getPager Data($total, $limit, $page);
    $offset= $pager->offset;
    $limit= $pager->limit;
    $page= $pager->page;
    $error="";
    [/code]
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      i added this statement
      Code:
      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

      Comment

      • omerbutt
        Contributor
        • Nov 2006
        • 638

        #4
        also that when i click any category which does not have any product listed against it the line
        Code:
        $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

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

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

          Have you tried turning the error messages on?
          [code=php]ini_set('displa y_errors', true);
          error_reporting (E_ALL);[/code]


          Also:
          Code:
          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

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by omerbutt
            also that when i click any category which does not have any product listed against it the line
            Code:
            $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.

            Comment

            • omerbutt
              Contributor
              • Nov 2006
              • 638

              #7
              Originally posted by Atli
              And you just get an empty page... odd.

              Have you tried turning the error messages on?
              [code=php]ini_set('displa y_errors', true);
              error_reporting (E_ALL);[/code]
              YES i had already turned
              error_reporting (E_ALL)
              also i included
              [code=php]ini_set('displa y_errors', true);[/code]
              but there were no errors displayed

              Also:
              Code:
              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

              Comment

              • omerbutt
                Contributor
                • Nov 2006
                • 638

                #8
                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 ?

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Have you tried counting the rows?
                  [code=php]
                  if($result && mysql_num_rows( $result) > 0) {
                  mysql_do_whatev er_you_want($re sult);
                  }
                  else {
                  echo "Sorry. No results.";
                  }[/code]
                  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.

                  Comment

                  • omerbutt
                    Contributor
                    • Nov 2006
                    • 638

                    #10
                    Originally posted by Atli
                    Have you tried counting the rows?
                    [code=php]
                    if($result && mysql_num_rows( $result) > 0) {
                    mysql_do_whatev er_you_want($re sult);
                    }
                    else {
                    echo "Sorry. No results.";
                    }[/code]
                    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 :(

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      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.
                      [code=php]
                      $sql = "SELECT COUNT(*) FROM mytbl";
                      $result = mysql_query($sq l) or die(mysql_error ());
                      $count = mysql_result($r esult, 0, 0);

                      if($count > 0) {
                      // Do your thing here.
                      }
                      else {
                      echo "Sorry. No cookie for you.";
                      }[/code]
                      Just make sure the code inside the if block prints something :)

                      Comment

                      • omerbutt
                        Contributor
                        • Nov 2006
                        • 638

                        #12
                        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.
                        [code=php]
                        $sql = "SELECT COUNT(*) FROM mytbl";
                        $result = mysql_query($sq l) or die(mysql_error ());
                        $count = mysql_result($r esult, 0, 0);

                        if($count > 0) {
                        // Do your thing here.
                        }
                        else {
                        echo "Sorry. No cookie for you.";
                        }[/code]
                        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

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          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::getPager Data function should be declared static if you intend to use it like that.

                          Comment

                          • omerbutt
                            Contributor
                            • Nov 2006
                            • 638

                            #14
                            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::getPager Data 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.

                            Comment

                            • Atli
                              Recognized Expert Expert
                              • Nov 2006
                              • 5062

                              #15
                              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:
                              [code=php]
                              $total = mysql_result($r esult, 0, 0) or die(mysql_error ());[/code]
                              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.

                              Comment

                              Working...