How do you send email when an mssql_connect fails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

How do you send email when an mssql_connect fails

Post by tdelobe »

Running PHP 4 with IIS and SQL Server

I want to send an email to myself anytime the connection to the database goes down. Currently I have this code...

Code: Select all

<?php
$link = mssql_connect( $d["db_ip"], $d["db_un"], $d["db_pw"]) or die ("<p>Could not connect to the database. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>");
        mssql_select_db( $d["db_db"], $link) or die ("<p>Could not select the database. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>");
?>
As you can see it currently just displays text. Is there any way to do that AS WELL AS send me an email notifying me the connection is dead? If so can you provide a code snippet. Thanks!
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

try something like this after your code

Code: Select all

if (!$link) mail('your@email.com','Problem with database', 'Some email body, maybe using the date() function to show the current date');
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

You can also do it by writing your own custom error handler. Here is a copy of ours, we just include this on every page. It gives us info abour the user and the last query that was ran. Also no need to add special code to each call.

If it's on the production site it mails us and gives a nice message to the user, if it's on the test system it just displays the error to the screen.

Code: Select all

<?php
 set_error_handler("userErrorHandler");

 function userErrorHandler ($errno, $errmsg, $filename, $linenum, $vars) {
   global $db;
   if ($errno!=8 && error_reporting()!=0) {
     ob_start();
     echo '<br>=============================================================================================================<br>';
     echo date("d M Y H:i:s") . ' - ' . $errno . ' - ' . $errmsg . ' in ' . $filename . ' line: ' . $linenum . '<br><br>';
     echo 'Script: <b>' . $_SERVER['PHP_SELF'] . '</b><br>';
     if (isset($_SESSION['session'])) echo 'User: <b>' . $_SESSION['session']->userName . '</b><br>';
     if (isset($db)) echo 'Last Query: <b>' . $db->q . '</b><br>';
     echo '=============================================================================================================<br>';
     $tmp = ob_get_contents();
     ob_end_clean();
     
     if (function_exists('dbClose')) dbClose();

     if (ISPRODUCTION) {
       mail('someone@somewhere.com','WebSite Error', $tmp, 'content-type: text/html');
       die('<br><hr><b>The page has encountered an error, Technical staff have been notified and the problem will be<br>fixed as soon as possible.</b><hr>');
     }
     else {
       echo $tmp;
       die;
     }
   }
 }
?>
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

didnt work for some reason

Post by tdelobe »

Ok, so I tried the first solution but it did not send me an email. And I know sendmail is fine becauase I have it working in other areas of the site. here is the entire function in case there is something in there scerwing this up.

Code: Select all

<?php
function getDbByKey( $dbkey ) {
        global $link;
        global $arr_db;

        $d=$arr_db[ $dbkey ];
        if ( !is_array( $d ) ) {
            die("<p>Could not select the database [$dbkey]. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>" );
        }
        $link = mssql_connect( $d["db_ip"], $d["db_un"], $d["db_pw"]) or die ("<p>Could not connect to the database. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>");
		if (!$link) mail("tdelobe@usta.org","Insert Subject Here", "Insert Email Body Here");
        mssql_select_db( $d["db_db"], $link) or die ("<p>Could not select the database. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>");
        $result = mssql_query("set textsize 65000", $link);
    }

?>
Am I missing something??
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

well that won't work because you die when it won't connect, wich exits the process.. it never gets to your code.

you need to change

Code: Select all

$link = mssql_connect( $d["db_ip"], $d["db_un"], $d["db_pw"]) or die ("<p>Could not connect to the database. If this problem is frequent, kindly notify <a href=mailto:webmaster@usta.org>webmaster@usta.org</a></p><br>");
to

Code: Select all

$link = @mssql_connect( $d["db_ip"], $d["db_un"], $d["db_pw"])
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

awesome! thanks.
Post Reply