Turn Off Warnings and Errors on PHP and MySQL

Turn off warnings and errors on PHP and MySQL

When you are sure your script is perfectly working, you can get rid of warning and notices like this: Put this line at the beginning of your PHP script:

error_reporting(E_ERROR);

Before that, when working on your script, I would advise you to properly debug your script so that all notice or warning disappear one by one.

So you should first set it as verbose as possible with:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

UPDATE: how to log errors instead of displaying them

As suggested in the comments, the better solution is to log errors into a file so only the PHP developer sees the error messages, not the users.

A possible implementation is via the .htaccess file, useful if you don't have access to the php.ini file (source).

# Suppress PHP errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

# Enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log

# Prevent access to PHP error log
<Files PHP_errors.log>
Order allow,deny
Deny from all
Satisfy All
</Files>

Suppress mysql errors on php

Try putting this at the top of your code:

ini_set("display_errors", "off");

And yes, adding @ at the beginning of functions etc, for example:

$query = @mysql_query("text..");

Should suppress errors.

Remove warning messages in PHP

You really should fix whatever's causing the warning, but you can control visibility of errors with error_reporting(). To skip warning messages, you could use something like:

error_reporting(E_ERROR | E_PARSE);

Suppress specific MySQL warning message

You can use set_error_handler and check for the specific message, then just return false in cases where you want the use default error handling

E.G

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
if (false === strpos($errstr, 'Headers and client library minor version mismatch')) {
return false;
}
});

php mysql_connect Warning disable

Yes, add an @ sign like so to suppress warning / error messages, then do the error once your own:

$dblink = @mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS);

if (!$dblink)
{
$dblink = @mysql_connect(DBHOST_REMOTE, DBUSER, DBPASS);
}

if (!$dblink)
{
$message = sprintf(
"Could not connect to local or remote database: %s",
mysql_error()
);
trigger_error($message);
return;
}

Take care that you need to handle all error reporting your own then. Such code is hard to debug in case you make a mistake.

Connect to database without writing warning/error messages

Remove the or die from the statement, which will stop the error message you are seeing.

Then if you want more friendly errors have a look at overriding the default php errors/exceptions.

User custom error handling set_error_handler and exception handler set_exception_handler

PHP does not hide mysql_*() warnings, even if error_reporting(0) called

Posted on behalf of OP:

Finally I have found an answer for this.

I had set error_reporting hardly via php_admin_value in my Apache's httpd.conf and according to that, any lower-level configuration (and also script itself) had been unable to override that setting.

I just commented out the directive and everything works as expected.



Related Topics



Leave a reply



Submit