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?

Carl
  • 33
  • 9
  • 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 – Goikiu Jun 16 '15 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? – deceze Jun 16 '15 at 10:30
  • 1
    `mysql_error` doesn't *show* errors. It returns a string. `echo`ing or `print`ing 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. – Quentin Jun 16 '15 at 10:31
  • example change `echo mysql_error();` to `mysql_error()` or remove it. Voila it works. – Robert Jun 16 '15 at 10:32
  • Sorry, in the projects there are lots of `mysql_query() OR die(mysql_erro())`, I accidentally removed it editing the question – Carl Jun 16 '15 at 10:32
  • remove die(); you should really do it because I guarantee you that the logic of application will fail. Better fix problems and what's even more useful change engine to PDO or mysql because mysql_* functions are deprecated and will be removed. – Robert Jun 16 '15 at 10:33
  • Your bigger problem is that your error handling strategy is somewhere between horrible and non-existent. Silencing `mysql_error` is a drop in the bucket. – deceze Jun 16 '15 at 10:34
  • @Carl - I don't know why you voted on my answers so repeatedly but you have caused me some problems with moderators, please read [the rules](http://stackoverflow.com/help/serial-voting-reversed) and don't do it again, I just want to give my answers, not being targeted. Thanks – Cliff Burton Feb 26 '16 at 11:57

2 Answers2

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.

DocRattie
  • 1,392
  • 2
  • 13
  • 27
  • 1
    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 Jun 16 '15 at 10:52
  • 2
    @Carl - as I said in my answer, if you want to make a good project, then you must work for a lot of time. – Cliff Burton Jun 16 '15 at 10:54
  • @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. – DocRattie Jun 16 '15 at 10:59
  • 1
    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 Jun 16 '15 at 11:27
  • @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. :) – DocRattie Jun 16 '15 at 11:44
  • Oh, thats right! So I'll use your function with the logging-error articles, Thank you all! *-* – Carl Jun 16 '15 at 11:51
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.

Community
  • 1
  • 1
Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
  • 1
    Thanks, I had a look to the articles and they seem interesting and useful, I'm reading them. – Carl Jun 16 '15 at 10:45
  • 2
    You are welcome, but read DocRattie's answer too, it can be of help for you ;) – Cliff Burton Jun 16 '15 at 10:49
  • 1
    I read the articles, they are very interesting and useful, as soon as I reach 15 reputation I'll upvote your answer – Carl Jun 16 '15 at 11:26