Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Disable mysql error() for public site

Don't want users to see MySQL error messages

3:54 am on Aug 9, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
votes: 0

My question is quite simple. How can I disable mysql_error() so that my users never see SQL errors when they occur? I know I could simply remove or die(mysql_error()) from my code, but that would take a lot of time and I think that actually helps me see errors when I am developing.

Thanks guys!

12:29 pm on Aug 9, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2006
votes: 0

You have SQL errors on a live site?

I personally avoid 'or die()' statements, IMO it's an unelegant way to handle errors, perhaps analagous to 'or crash and burn()'.

One way would be to do a site-wide find/replace on your files.

12:41 pm on Aug 9, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 17, 2002
votes: 5

If the errors occur on a mysql function then put an @ in front of it.

$result = @mysql_query('.....

1:37 pm on Aug 9, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
votes: 0

I've written my database class in such a way that errors are saved in a variable and also stored in an array in case there may be multiples. I can check $db->lastErr to make sure it's 0 before going on.

If I display an error it's a generic "there was a problem with the database" unless a constant DEBUG_MODE is set to true in which case the code outputs the actual error.

2:17 pm on Aug 9, 2008 (gmt 0)

New User

5+ Year Member

joined:Aug 9, 2008
votes: 0

use @ ?
8:11 pm on Aug 9, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Jan 16, 2007
votes: 0

If your problem is with or die() there's no way around removing them. But if you're still triggering them, you're not finished developing ;)

Welcome to WebmasterWorld, mimizzee.
Placing an @ in front of a function call suppresses error messages. Use it carefully; you can bewilder yourself if problems occur and you don't see the errors that are being generated.

12:55 pm on Aug 10, 2008 (gmt 0)


WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
votes: 0

change all your or die statements to a custom logging function. Then if you get an error you will still know about it.

>> but that would take a lot of time

ummm, yes it would but you can do something correctly, or not, whichever you like.

If you want a good practice for future projects

build in an error function that you can use instead of die() then you can have it echo mysql_error() while in dev. When you move it to live you can change it in a single spot and change to some type of logging.

5:58 pm on Sept 26, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 3, 2007
votes: 0

Greetings guys and gals,

I know I am over 40 days late here =P, but thank you all for replying to this thread. My question was really dumb anyway, I actually found a simple solution all by myself about a month ago, but forgot to report it here:

1. I created 1 new Constant and used quite a bit one constant I had already created:
- SITE_PATH (had created it a while ago already) = can be either "http://www.example.com/" or "http://localhost/". It will change depending on which server I am on (locally or just in my Web Hosting company's server).
- MY_IP = my own IP address, which never changes.
2. I created a new function in PHP called isLocalhost(). Basically it returns TRUE if the IP corresponds to my own IP address and if the SITE_PATH is equal to "localhost".

Since I have one simple PHP class called Query to perform any database functions, I changed one part of my code to the following simple IF statement to show errors ONLY IF I am visiting my site:


if (isLocalhost()) {
mysql_query($this->query) or die( mysql_error() );
} else {


Where "$this->query" is the variable with my MySQL Query. Eg: "SELECT * FROM users".

It worked pretty well. I wrote a database query that did not make sense just to test whether the error would show up or not. And it did show up. I went to a Proxy website and visited my own site in it to see if the errors would show up, and as expected, they did not. So as you can see this is pretty straight forward.

Again, this is a pretty simple solution. It may take some time for some people to make the necessary changes, but if you have a PHP class Database abstraction layer like me, I'm sure it will be pretty easy to make this change in your code.

Hope this helps someone too.