1

I came across in a project of about 900 files with 3 millions lines of code, and my boss asked me to find a solution to prevent mysql_error() to show errors.

This is the syntax mysql_query() OR die(mysql_error())

So, how can I disable the mysql_error() from showing errors?

8
  • 1
    if a line of code can launch an error you can try to put @ before the function to suppress it if i recall correctly Commented Jun 16, 2015 at 10:26
  • 1
    What does that mean? mysql_error itself is not "showing errors". Does that mean you have a ton of echo mysql_error() everywhere throughout your code and you want to silence that? Commented Jun 16, 2015 at 10:30
  • 1
    mysql_error doesn't show errors. It returns a string. echoing or printing that error will display it. You need to fix the root cause (which is, frankly going to be a hell of a lot of work with that much code to dig through) — you will regret any shortcuts you take there. While you are at it, you should stop using the mysql_ extension (which is deprecated and will be removed from PHP in the future) and upgrade to PDO or the mysqli_ extension. Commented Jun 16, 2015 at 10:31
  • example change echo mysql_error(); to mysql_error() or remove it. Voila it works. Commented Jun 16, 2015 at 10:32
  • Sorry, in the projects there are lots of mysql_query() OR die(mysql_erro()), I accidentally removed it editing the question Commented Jun 16, 2015 at 10:32

2 Answers 2

2
function mysql_own_error($debug = false){
   $msg = mysql_error();
   if($debug){
     echo $msg;
   }else{
     // Function to log errors here.
   }
}

With that you can set a global debug-variable $debug. Only if that is true output the error msg. Now replace every mysql_error() with mysql_own_error($debug). There are Editors that can do suche replaces fast.

With that you will prevent the mysql_error() from showing errors publicly but you can still debug the code if you need to.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Mr for your answer, but I should include a file containing the variable in all files because in the project there is no file which is included in ALL the other files
@Carl - as I said in my answer, if you want to make a good project, then you must work for a lot of time.
@Carl If you have no central config-file you should defenetly include one. With 3mio lines of code it will hardly be possible to manage errors without a config-file. And beside that it will help you overwall when you face other problems.
Thank you for your help, but I read @CliffBurton's linked articles and now I think that keeping logging errors without showing them to the user is a better solution. But your answer was helpful too, as soon as I reach 15rep i'll upvote your answer
@Carl you could use the else-part as the place where to log errors. I edit it to show that. just as an idea on how to start with that. :)
|
1

If you're still having errors then your project is not finished yet.

One way would be to do a site-wide find/replace on your files and replace the OR die(mysql_error()) with an @ in front of mysql_error() like so: OR die(@mysql_error()).

Placing an @ in front of a function call suppresses error messages. But use it carefully, this is not always a good solution.

Read this post which links to this article to know if it's a good solution for you.

I would change all OR die() occourrences to a custom error-handling function, then if you get an error you will still know about it without displaying them to users.

Yes, it would take a lot of time, but a good project takes a lot of time.

Check this article to create your own error-handling function and this other one to Enable PHP error logging via .htaccess, they really helped me.

3 Comments

Thanks, I had a look to the articles and they seem interesting and useful, I'm reading them.
You are welcome, but read DocRattie's answer too, it can be of help for you ;)
I read the articles, they are very interesting and useful, as soon as I reach 15 reputation I'll upvote your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.