473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Sep 25 '06 #1
7 3298
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
Sep 25 '06 #2
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
Sep 25 '06 #3
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
*****************************
Sep 26 '06 #4
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

}
}
Sep 26 '06 #5
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
Sep 26 '06 #6
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('m*@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 <11*********************@i42g2000cwa.googlegroups. com>,
mc******@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

Sep 27 '06 #7
Klaus Brune wrote:
In article <11*********************@i42g2000cwa.googlegroups. com>,
mc******@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('m*@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.
js*******@attglobal.net
==================
Sep 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joshua Beall | last post by:
Hi All, I know that I can prepend functions with an '@' character in order to get them to not output any errors, but I am wondering is there any way for a function that I write to know if it has...
2
by: erica | last post by:
Hi, I am currently writing PHP code for some polling software. When someone votes, it stores their IP address in the database. From then on, they cannot vote in that particular poll, they only...
1
by: Liam Caffrey | last post by:
Hi, I can see that by using the object ID rather that the object name, the following SQL query works. Has anybody got any idea what is causing the error? -- Works OK select o.id...
14
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
2
by: agphoto | last post by:
when we do mysql_query with while and mysql_num_rows() together then while do no show the first row from table in database.. example : <? $conn = mysql_connect($DBhost,$DBuser,$DBpass) or...
9
by: Cybex | last post by:
I am trying to get this to work but when ever I enter an proper integer it just hangs. The Switch default seems to catch the improper integers but the right ones are not triggering the way I...
2
by: drgnhiker | last post by:
And I seem to be missing the error...can't find it and the statements in one section duplicate other sections with no error....any help would be greatly appreciated: the following is the entire...
2
by: TxSteve | last post by:
Hello (I'm a rookie) and need help please understanding why my last row will not total. Using SharePoint (MOSS 2007) built a data view and edited the stock code using this helpful advice;...
13
Topbidder
by: Topbidder | last post by:
I have this error on the code Parse error: syntax error, unexpected '"' in /home/topbidd/public_html/bid2/bid_classic.php on line 159 now i thought the error was this It seems that the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.