Jump to content

MySQL Error Handling


Altec

Recommended Posts

I really don't know how to explain my problem without posting code first, so bear with me. :)

 

// Connect to the database
if(!mysql_connect('localhost', DB_USER, DB_PASS)) {
    $page = new Page('error.html');
    $page->replace_tags(array(
        'TYPE_OF_ERROR'     => $string['DATABASE_CONNECT_ERROR'],
        'ERROR_CONTENT'     => mysql_error(),
    ));
    $page->output();
    exit;
}

 

All that does is attempt to connect to the MySQL database. If it fails it outputs an error page (error.html). However, it automatically outputs an error THEN outputs the error page, which results in this:

 

<br />

<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'DB_USER'@'localhost' (using password: YES) in <b>post.php</b> on line <b>10</b><br />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

How can I prevent (or "catch") the error from being displayed?

Link to comment
https://forums.phpfreaks.com/topic/129347-mysql-error-handling/
Share on other sites

On a live server, the display_errors setting should be OFF.

 

On a development server the display_errors setting should be ON and the additional information that is in the messages should be used to help you in fixing the problem in your code. Code does not normally generate any errors, only for unexpected conditions.

Link to comment
https://forums.phpfreaks.com/topic/129347-mysql-error-handling/#findComment-670578
Share on other sites

If the master php.ini is not accessible, the setting can be turned off in a .htaccess file (when php is running as an Apache module) or in a local php.ini (when php is running as a CGI application.)

 

Turning it off in each of your files is just extra work, you might forget to do it in some files, and if you need to turn it on, you would need to change each affected file, because the setting in a script overrides any global setting.

Link to comment
https://forums.phpfreaks.com/topic/129347-mysql-error-handling/#findComment-670599
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.