#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Argenta, BC, Canada
    Posts
    15
    Rep Power
    0

    Custom error message for MySQL connection


    Hi all,

    I've created a MySQL connection - as a PHP include for security. So the connection itself is separate from the page it is called from. There's only one simple use for this database connection so far- to pull a specific value for a hotel price from a table and insert it in into a hotel detail page. I'd like to create custom error messages for those (hopefully rare) occassions when the database cannot be accessed for some reason.

    The way it is now, if the connection doesn't work, there's a string of error messages like this:

    PHP Code:
     Parse errorparse error in /myconnection/include/path on line 7

    Warning
    mysql_query(): supplied argument is not a valid MySQL-Link resource in /mysite/directory/page.php on line 413

    Warning
    mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /mysite/directory/page.php on line 414

    Warning
    mysql_close(): supplied argument is not a valid MySQL-Link resource in /mysite/directory/page.php on line 416 
    -------
    In other words, if the connection fails, there's a parse error message, then all the other variables called by the queries each give error messages.

    I could change the connection to die if the connection fails - BUT, I don't want PHP to stop parsing the whole page just because the connection failed. I'd like a simple error message like "Temporarily unavailable' to print where the price value should go, and the rest of the page be rendered as normal. (Gosh, I hope that's clear!)

    Here's how the connection script is called, with a sample SQL query:

    PHP Code:
     <?php 
            
    include('MyConnection.php');
    $query "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
    $result mysql_query($query$dbConn);
    $price mysql_fetch_row($result);
    echo 
    $price[0];
    mysql_close($dbConn);
    ?>
    Here's my connection script

    PHP Code:
    <?php
     $host 
    'mymysqlserver';
              
    $dbUser 'myuser';
              
    $dbPass 'mypassword';
              
    $dbName 'mydbname';
              if (!
    $dbConn mysql_connect($host$dbUser$dbPass)) {
              die(
    'Connection Temporarily Unavailable');
              }
              if (!
    mysql_select_db($dbName$dbConn)) {
              die(
    'Temporarily Unavailable');
              }
              
    ?>
    Is there a way to have the non-databased part of the page rendered normally, and a simple, user-friendly error message printed if the connection fails?
  2. #2
  3. Trapped on the forums...help
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2003
    Location
    /Users/edman007
    Posts
    4,602
    Rep Power
    910
    first i would set all error reporting to either off of log it all (it all goes in the webserver error log) that would take care of all the errors that php gives, then instead of dieing when the connection fails have it set some variable to '1' or something, then in all your scripts where you have all the stuff that gets the data out of the database put it all in an if else statement based on the variable you set in the connection script, if set to one have it skip all the database stuff and use predefined stuff (like "temporality unavailable") instead of the database values
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Argenta, BC, Canada
    Posts
    15
    Rep Power
    0
    Thanks, edman007.
    I can see your logic. Do I turn off the error messages in php.ini? (not sure how to do that).

    If I create a variable like $dberr, that would be declared in my included connection script, right?
  6. #4
  7. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    902
    the script you have pasted should work fine. did you edit it before posting? do any of your variables (host, pass, etc) have an apostrophe that you did not escape? you're getting a parse error which does not mean that it's not connecting right.. it means you have a type somewhere or incorrect syntax..

    however, if the script is exactly how you pasted (the one that makes the connection, not the one that calls it), it should work fine. it would print the die() error messages that it should at the appropriate time.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Argenta, BC, Canada
    Posts
    15
    Rep Power
    0
    Hi angrypanda.

    Yes, I edited the script. The script works fine to access the database; what I want is an elegant way to handle occassions when the database might be unreachable for whatever reason. So I intentionally changed the value of a connection variable, to see how the script would respond - hence all the parse errors.

    What I'd like is if the database can't be accessed, the script will simply print a user-friendly message, and the rest of the page (which is static html) will be rendered as normal. Gosh, I hope that's clear :-)
  10. #6
  11. Trapped on the forums...help
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2003
    Location
    /Users/edman007
    Posts
    4,602
    Rep Power
    910
    here is a sample of what to do, this will not help a parse error

    this would be the connection script
    PHP Code:
    <?php
    $host 
    'mymysqlserver';
    $dbUser 'myuser';
    $dbPass 'mypassword';
    $dbName 'mydbname';
    $mysql_failed FALSE;
    $dbConn mysql_connect($host$dbUser$dbPass);
    if (!
    $dbConn) {
        
    $mysql_failed TRUE;
    }
    if (!
    mysql_select_db($dbName$dbConn)) {
        
    $mysql_failed TRUE;
    }
    ?>
    and this would be the file using the connection
    PHP Code:
    <?php
    include('MyConnection.php');
    if (
    $mysql_failed){
        echo 
    'Temporally Unavailable';
    } else {
        
    $query "SELECT doubleratemidweek FROM price WHERE DatabaseRefNo = 112780";
        
    $result mysql_query($query$dbConn);
        
    $price mysql_fetch_row($result);
        echo 
    $price[0];
        
    mysql_close($dbConn);
    }
    ?>
    the parse error should never be happing it is a problem with you code, you have to fix that, once it is fixed you don't have to worry about getting a parse error because you would have to edit the file and mess it up, you won't get that if mysql goes down

    to stop the errors when mysql goes down you could either add a @ infront of any function that generates an error (like this @mysql_connect();) or add this to the top of any script
    PHP Code:
    ini_set('display_errors',0);
    ini_set('log_errors',1); 
    this will send all errors to the webserver logs instead of the browser, it is probably a little better than the @ because it will log anytime that mysql goes down, but it won't work for parse errors, for them it has to be in the php.ini file

    [edit]had to disable smilies[/edit]
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Argenta, BC, Canada
    Posts
    15
    Rep Power
    0
    Hey thanks! That looks terrific. Thanks for explaining everything so clearly; I really appreciate your help!

IMN logo majestic logo threadwatch logo seochat tools logo