Bluehost.com Web Hosting $6.95

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

This is a discussion on while ($row = mysql_fetch_array($result)) error suppression within the PHP Language forums, part of the PHP Programming Forums category; Hi all! How can you get rid of the error that displays if you do a query which returns no ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-25-2006
monomaniac21
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 09-25-2006
Oli Filth
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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
Reply With Quote
  #3 (permalink)  
Old 09-25-2006
Oli Filth
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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
Reply With Quote
  #4 (permalink)  
Old 09-26-2006
Chuck Anderson
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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
http://www.CycleTourist.com
*****************************
Reply With Quote
  #5 (permalink)  
Old 09-26-2006
friglob
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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($query);
$num = mysql_num_rows($result);

if( $num ) {

while ($row = mysql_fetch_array($result)){

//whatever

}
}
Reply With Quote
  #6 (permalink)  
Old 09-26-2006
Oli Filth
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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($query);
> $num = mysql_num_rows($result);
>
> if( $num ) {
>
> while ($row = mysql_fetch_array($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_array() will return false, and the while loop will stop.

--
Oli
Reply With Quote
  #7 (permalink)  
Old 09-27-2006
Klaus Brune
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($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...

$oldErrorReporting = error_reporting(0)

// ... do your stuff ...

error_reporting($oldErrorReporting)


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

ob_start();
$oldErrorReporting = error_reporting(E_ALL);
$oldHtmlErrors = ini_set('html_errors',0);

// ... do your stuff ...

$errorMessages = ob_get_clean();
error_reporting($oldErrorReporting);
ini_set('html_errors',$oldHtmlErrors);

if(trim($errorMessages) == '') {
mail('me@example.com','MySQL Error Report',$errorMessages)
}


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

http://www.gunthersoft.com/wiki/ErrorHandlingWithPHP




In article <1159199002.996276.77090@i42g2000cwa.googlegroups. com>,
mcyi2mr3@googlemail.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
>
>

Reply With Quote
  #8 (permalink)  
Old 09-27-2006
Jerry Stuckle
 
Posts: n/a
Default Re: while ($row = mysql_fetch_array($result)) error suppression

Klaus Brune wrote:
> In article <1159199002.996276.77090@i42g2000cwa.googlegroups. com>,
> mcyi2mr3@googlemail.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...
>
> $oldErrorReporting = error_reporting(0)
>
> // ... do your stuff ...
>
> error_reporting($oldErrorReporting)
>
>
> 2) Do output buffering before and after your code to grab any unwanted
> errors, notification, etc...
>
> ob_start();
> $oldErrorReporting = error_reporting(E_ALL);
> $oldHtmlErrors = ini_set('html_errors',0);
>
> // ... do your stuff ...
>
> $errorMessages = ob_get_clean();
> error_reporting($oldErrorReporting);
> ini_set('html_errors',$oldHtmlErrors);
>
> if(trim($errorMessages) == '') {
> mail('me@example.com','MySQL Error Report',$errorMessages)
> }
>
>
> 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...
>
> http://www.gunthersoft.com/wiki/ErrorHandlingWithPHP
>
>


(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@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:22 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0