Page 1 of 1
How do you send email when an mssql_connect fails
Posted: Thu Mar 04, 2004 11:16 am
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!
Posted: Thu Mar 04, 2004 11:33 am
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');
Posted: Thu Mar 04, 2004 11:50 am
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;
}
}
}
?>
didnt work for some reason
Posted: Thu Mar 04, 2004 12:00 pm
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??
Posted: Thu Mar 04, 2004 12:07 pm
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"])
Posted: Thu Mar 04, 2004 2:59 pm
by tdelobe
awesome! thanks.