user1482117
user1482117

Reputation: 45

Error page for MySQL errors

I was wondering if there is any possible way to make MySQL errors cleaner. It's not a very big issue, but if they do happen I'd like them to be nice and neat.

I did read one tutorial, which sadly didn't work, but they did seem to give me hope for a possibility.

I would like my MySQL errors, such as "no connection," to show a error page like a 404 error page.

Anyone know? working tutorials?

Here is the tutorial I read: http://www.jooria.com/Making-Error-Page-When-The-Mysql-Offline-a66.html

Upvotes: 2

Views: 2284

Answers (2)

Peon
Peon

Reputation: 8020

Add @ to hide output:

<?php
    $host = "localhost";
    $dbuser = "*****";
    $dbpwd = "*****";
    $db = "*****";

    $connect = @mysql_pconnect( $host, $dbuser, $dbpwd );
    if( !$connect ) {
        header( 'Location: /custom-404-page.php' ); die;
    } else {
        $select = @mysql_select_db($db);
    }
?>

Something like this should work.

$result = @mysql_query ($query, $connection );
if ( mysql_error( $connection ) != "" ) {
    header( 'Location: /custom-404-page.php' ); die;
}

Or, if you have already headers set, replace header( 'Location: /custom-404-page.php' ); with:

echo '<script>window.location.href="custom-404-page.php";</script>';

!Caution: Placing an @ in front of a function call suppresses all error messages. Use it carefully; you can think all is working, when it actually isn't!

Upvotes: 3

zambesianus
zambesianus

Reputation: 1249

You can do like:

$connect = mysql_pconnect($host, $dbuser, $dbpwd);
if(!$connect) {
header('Location: /noconnection.html');
exit();
}

If you wanted to do exceptions simply

$mysqli = new mysqli($address,$username,$password,$database);
if ($mysqli AND $mysqli->connect_error)
throw new Exception("DB Error ($mysqli->connect_errno): $mysqli->connect_error);

Upvotes: -1

Related Questions