Supressing MySQL errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Supressing MySQL errors

Post by MarkAshley »

Hi all

I am developing a web app which runs on PHP/MySQL/Apache. If the results of a query include a field with a value of null, mysql_result() returns an invalid result source error. I don't want my users to see the error, as the query returning a null field is not a problem.

In php.ini I have set error_reporting to E_COMPILE_ERROR as this seems to be the least likely to include this error. I also set display_errors to Off. I also added error_reporting(0) to the top of my script. However, the errors still appear.

Is there anything else I need to do in order to stop these MySQL errors from appearing?

Thanks
Mark
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If the results of a query include a field with a value of null, mysql_result() returns an invalid result source error.
Probably something went wrong with the query (e.g. a sql syntax error), therefore the first parameter for mysql_result is not a mysql result resource but FALSE.
The exact error/warning message for this is
Warning: mysql_result(): supplied argument is not a valid MySQL result resource
There's no problem fetching a NULL value with mysql_result.
donvliet
Forum Newbie
Posts: 7
Joined: Mon Aug 07, 2006 3:33 pm
Location: Luxembourg

About error_reporting

Post by donvliet »

Do yourself the favor and switch your error_reporting on (E_ALL), display errors of all kinds and try a different approach to get your data from the database.
Switching off errors during development is the worst thing you can do.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

well i dont advice as i learn in this forum, but for instant solution you can use @mysql_result

using '@' before the function will supreme all errors, but remember if there is a error coz something is wrong...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Just put single quotes around the value so nulls work. MySQL will sort out the type based on the column type.
(#10850)
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Thanks for the replies. I made a mistake in my original post. I was using mysql_result() when the error occurred. It occurred when no results were returned from the query so row 0 did not exist, but I tried to assign the value of row 0 to a variable. This is not a problem as an empty result is normal in this application. It just means that a field on the form will be empty instead of containing data.

I am aware of the dangers of turning off errors, but I didn't want my users to be bombarded with irellevant error messages. Adding an @ to the beginning of the mysql_result() functions works perfectly for suppressing these errors.

Thanks very much!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

mysql_result() is the worst, slowest, most awful function in PHP. Use *anything* else.
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

What is the alternative?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_*
Post Reply