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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 ***************************** |
|
|||
|
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 } } |
|
|||
|
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 |
|
|||
|
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 > > |
|
|||
|
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 ================== |