Redirect when sql server fails

mkrkarthi

New Member
Messages
18
Reaction score
0
Points
1
I want to redirect or display a custom message when mysql server fails.
Also, I want to hide the default sql server error message. How to do it?

For example i want to redirect the users to blogger blog when accessing my wordpress blog at the time of sql server fault.

Is there any possiblities to keep a catch copy of entire website to browse without using mysql database?


I am new to php programming..
Please help me..
Thanks in advance
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
This is how I do it. I've created a separate file to connect to the mysql database, so on any page I want to connect to the server I just include that script (in my case "conf/mysql_connect.php"). The error page is "error/database.php". As long as you use this script before any output it's guaranteed to display only the error page when the database fails.

PHP:
<?php
@mysql_connect("server", "username", "password") or databasefailure();
@mysql_select_db("dbname") or databasefailure();

function databasefailure() {
	include "{$_SERVER["DOCUMENT_ROOT"]}/error/database.php";
	exit;
}
?>

This code can be easily adapted to suit your needs (actually I just removed some error-logging code from it just now, so I can show you the essence).

Also, notice the '@' in front of those two lines. Using this character will cause the line to become silent. No run-time errors will be displayed for that line, at all. (Of course syntax errors are still displayed.) Be careful with this though, it will hide any warning or error for that line, which can cause a lot of headache when debugging ;)

Edit:
about the cache copy:
Theoretically, I could come up with some methods. Practically there are some other problems, although not impossible. Unless it's critical your site has this ability, I'd suggest you don't bother and spend the time you save on improving your site for when it doesn't have a database-failure :)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Alternatively, you can work with PDO and exceptions, which has the advantages of not disclosing too much information to users, yet still making that info available for you to log:
PHP:
<?php
include_once('localDB.php');

try {
    $dbConn = localDB();
    
    $query = $dbConn->prepare(...);
    $query->execute(...);
    foreach ($query as $row) {
        ...
    }
} catch (PDOException $exc) {
    error_log($exc);
    header('Location: ...'); // This only works if nothing has been outputted.
    // OR
    include(...);
} ?>
You could also include scaffolding to print the exception (rather than redirect) when a variable is set or logged on as an admin. Be careful with this: the exception information includes the call stack. If the error happens when creating a PDO object, the username & password appear on the call stack.

localDB.php handles DB connections and bundles up credentials:
PHP:
<?php
function localDB($dbName='...') { // replace '...' with default DB name
    static $db = array();
    if (empty($db[$dbName])) {
        $db[$dbName] = new PDO("mysql:host=localhost;dbname=$dbName", 'user', 'password');
    }
    $db[$dbName]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    return $db[$dbName]; 
}
?>
 
Top