while ($row = mysql_fetch_array($result)) error suppression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • monomaniac21

    #1

    while ($row = mysql_fetch_array($result)) error suppression

    Hi all!

    How can you get rid of the error that displays if you do a query which
    returns no result and then try and fetch the array, WITHOUT having to
    put the while in another conditional like if ($result != ''), something
    more efficient if possible.

    regards

    Marc

  • Oli Filth

    #2
    Re: while ($row = mysql_fetch_arr ay($result)) error suppression

    monomaniac21 said the following on 25/09/2006 16:43:
    Hi all!
    >
    How can you get rid of the error that displays if you do a query which
    returns no result and then try and fetch the array, WITHOUT having to
    put the while in another conditional like if ($result != ''), something
    more efficient if possible.
    A query will only return no result if the syntax is invalid.


    --
    Oli

    Comment

    • Oli Filth

      #3
      Re: while ($row = mysql_fetch_arr ay($result)) error suppression

      Oli Filth said the following on 25/09/2006 18:15:
      monomaniac21 said the following on 25/09/2006 16:43:
      >Hi all!
      >>
      >How can you get rid of the error that displays if you do a query which
      >returns no result and then try and fetch the array, WITHOUT having to
      >put the while in another conditional like if ($result != ''), something
      >more efficient if possible.
      >
      A query will only return no result if the syntax is invalid.
      >
      That is, if the SQL syntax is invalid.


      --
      Oli

      Comment

      • Chuck Anderson

        #4
        Re: while ($row = mysql_fetch_arr ay($result)) error suppression

        monomaniac21 wrote:
        Hi all!
        >
        How can you get rid of the error that displays if you do a query which
        returns no result and then try and fetch the array, WITHOUT having to
        put the while in another conditional like if ($result != ''), something
        more efficient if possible.
        >
        regards
        >
        Marc
        >
        >
        You can always append the mysql_query with @, but then you're only
        hiding a real problem.

        Query's that return no results are not erroneous, they do not display an
        error message. If you get an error message it means your query was
        invalid. echo mysql_error() to find out why.

        --
        *************** **************
        Chuck Anderson • Boulder, CO

        *************** **************

        Comment

        • friglob

          #5
          Re: while ($row = mysql_fetch_arr ay($result)) error suppression

          monomaniac21 wrote:
          Hi all!
          >
          How can you get rid of the error that displays if you do a query which
          returns no result and then try and fetch the array, WITHOUT having to
          put the while in another conditional like if ($result != ''), something
          more efficient if possible.
          >
          regards
          >
          Marc
          >

          $result = mysql_query($qu ery);
          $num = mysql_num_rows( $result);

          if( $num ) {

          while ($row = mysql_fetch_arr ay($result)){

          //whatever

          }
          }

          Comment

          • Oli Filth

            #6
            Re: while ($row = mysql_fetch_arr ay($result)) error suppression

            friglob said the following on 26/09/2006 11:09:
            monomaniac21 wrote:
            >>
            >How can you get rid of the error that displays if you do a query which
            >returns no result and then try and fetch the array, WITHOUT having to
            >put the while in another conditional like if ($result != ''), something
            >more efficient if possible.
            >>
            >
            $result = mysql_query($qu ery);
            $num = mysql_num_rows( $result);
            >
            if( $num ) {
            >
            while ($row = mysql_fetch_arr ay($result)){
            >
            //whatever
            >
            }
            }
            But why bother? Both these tests do the same thing. If there are no
            results (i.e. no rows returned), then the first call to
            mysql_fetch_arr ay() will return false, and the while loop will stop.

            --
            Oli

            Comment

            • Klaus Brune

              #7
              Re: while ($row = mysql_fetch_arr ay($result)) error suppression

              I don't know if this will be more efficient than using a conditional,
              but two other avenues, off the top of my head...

              1) Turn off error messages, then enable again after your call. Something
              like...

              $oldErrorReport ing = error_reporting (0)

              // ... do your stuff ...

              error_reporting ($oldErrorRepor ting)


              2) Do output buffering before and after your code to grab any unwanted
              errors, notification, etc...

              ob_start();
              $oldErrorReport ing = error_reporting (E_ALL);
              $oldHtmlErrors = ini_set('html_e rrors',0);

              // ... do your stuff ...

              $errorMessages = ob_get_clean();
              error_reporting ($oldErrorRepor ting);
              ini_set('html_e rrors',$oldHtml Errors);

              if(trim($errorM essages) == '') {
              mail('me@exampl e.com','MySQL Error Report',$errorM essages)
              }


              Hmmm, this might be worthy of a wiki topic. Plus it might be a little
              clearer with syntax highlighting. And I won't have to post multiple
              times if I want to expand the whole error trapping idea when I have
              time. The ideas are flowing already... dumping to different error log
              files based on what routine(s) you're watching for instance. Anyway,
              here's the link...






              In article <1159199002.996 276.77090@i42g2 000cwa.googlegr oups.com>,
              mcyi2mr3@google mail.com says...
              Hi all!
              >
              How can you get rid of the error that displays if you do a query which
              returns no result and then try and fetch the array, WITHOUT having to
              put the while in another conditional like if ($result != ''), something
              more efficient if possible.
              >
              regards
              >
              Marc
              >
              >

              Comment

              • Jerry Stuckle

                #8
                Re: while ($row = mysql_fetch_arr ay($result)) error suppression

                Klaus Brune wrote:
                In article <1159199002.996 276.77090@i42g2 000cwa.googlegr oups.com>,
                mcyi2mr3@google mail.com says...
                >
                >>Hi all!
                >>
                >>How can you get rid of the error that displays if you do a query which
                >>returns no result and then try and fetch the array, WITHOUT having to
                >>put the while in another conditional like if ($result != ''), something
                >>more efficient if possible.
                >>
                >>regards
                >>
                >>Marc
                >>
                >>
                I don't know if this will be more efficient than using a conditional,
                but two other avenues, off the top of my head...
                >
                1) Turn off error messages, then enable again after your call. Something
                like...
                >
                $oldErrorReport ing = error_reporting (0)
                >
                // ... do your stuff ...
                >
                error_reporting ($oldErrorRepor ting)
                >
                >
                2) Do output buffering before and after your code to grab any unwanted
                errors, notification, etc...
                >
                ob_start();
                $oldErrorReport ing = error_reporting (E_ALL);
                $oldHtmlErrors = ini_set('html_e rrors',0);
                >
                // ... do your stuff ...
                >
                $errorMessages = ob_get_clean();
                error_reporting ($oldErrorRepor ting);
                ini_set('html_e rrors',$oldHtml Errors);
                >
                if(trim($errorM essages) == '') {
                mail('me@exampl e.com','MySQL Error Report',$errorM essages)
                }
                >
                >
                Hmmm, this might be worthy of a wiki topic. Plus it might be a little
                clearer with syntax highlighting. And I won't have to post multiple
                times if I want to expand the whole error trapping idea when I have
                time. The ideas are flowing already... dumping to different error log
                files based on what routine(s) you're watching for instance. Anyway,
                here's the link...
                >

                >
                >
                (top posting fixed)

                Much less efficient. When you get the error, the MySQL libraries have
                to report it back to PHP, and PHP must handle the error. This may (and
                generally does) have significant overhead - like logging the error,
                determining if it can be handled or the script must be terminated, etc.

                A conditional is just a quick test.

                You should never plan on getting errors in your code and handling them
                like you're suggesting. If for no other reason than a new release may
                change the default error handling.

                And BTW - please don't top post. Thanks.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...