homepage Welcome to WebmasterWorld Guest from 54.161.147.106
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Disable mysql error() for public site
Don't want users to see MySQL error messages
cosmoyoda




msg:3719057
 3:54 am on Aug 9, 2008 (gmt 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!

 

darrenG




msg:3719185
 12:29 pm on Aug 9, 2008 (gmt 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.

Frank_Rizzo




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

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

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

Nutter




msg:3719208
 1:37 pm on Aug 9, 2008 (gmt 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.

mimizzee




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

use @ ?

cameraman




msg:3719417
 8:11 pm on Aug 9, 2008 (gmt 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.

jatar_k




msg:3719757
 12:55 pm on Aug 10, 2008 (gmt 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.

cosmoyoda




msg:3752879
 5:58 pm on Sep 26, 2008 (gmt 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:

<?php

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

?>

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.

Cosmoyoda

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved