+ Reply to Thread
Results 1 to 3 of 3

Thread: Redirect when sql server fails

  1. #1
    mkrkarthi's Avatar
    mkrkarthi is offline x10Hosting Member mkrkarthi is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    18

    Arrow Redirect when sql server fails

    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 by mkrkarthi; 01-19-2010 at 04:07 AM.

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Redirect when sql server fails

    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 Code:
    <?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 by marshian; 01-19-2010 at 04:52 AM.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Redirect when sql server fails

    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 Code:
    <?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 Code:
    <?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_ERRMODEPDO::ERRMODE_EXCEPTION); 
        return 
    $db[$dbName]; 
    }
    ?>
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. ABC'S of SMTP POP3 emal setup
    By zeppfrog in forum Tutorials
    Replies: 4
    Last Post: 11-22-2009, 07:20 PM
  2. Microsoft SQL Server (a rant)
    By merrillmck in forum Programming Help
    Replies: 6
    Last Post: 07-08-2009, 10:43 AM
  3. Free Teamspeak2 Virtual Private Server (VPS)
    By lokiklown in forum Gamer's Lounge
    Replies: 8
    Last Post: 01-03-2009, 06:12 PM
  4. sql server?
    By LarryXz in forum Free Hosting
    Replies: 2
    Last Post: 11-12-2007, 03:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers