IrOnMaSk Posted July 13, 2011 Share Posted July 13, 2011 hi guys, i'm just exploring the possibility. so i'm trying to send email alert to myself if the connection to the database fail by giving it the wrong password. I tried and the connection did fail but there's no email sent. So I don't know if it's even possible to do that or not or i'm just trying something that's not even exist? or it's possible but there's error in my code? here's the code <?php $conn = mysql_connect('localhost', 'root', 'wrongpass') or die(mysql_error()); $select = mysql_select_db('student') or die (mysql_error()); if(!$conn || !$select) { $to = '[email protected]; $message = 'can\'t connect or select the database'; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // More headers $headers .= 'From: <[email protected]>' . "\r\n"; mail($to,$subject,$message,$headers); } ?> appriciate it Quote Link to comment https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/ Share on other sites More sharing options...
dcro2 Posted July 13, 2011 Share Posted July 13, 2011 In your code, if either the connection fails or the select fails, your script outputs mysql_error() and then exits. It never gets to the part where you check if either of them failed. Take a look at the manual page for die (which is actually exit). Quote Link to comment https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/#findComment-1242351 Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 Not tested. <?php try { $conn = mysql_connect('localhost', 'root', 'wrongpass'); $select = mysql_select_db('student'); } catch { $to = '[email protected]; $message = 'can\'t connect or select the database'; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // More headers $headers .= 'From: <[email protected]>' . "\r\n"; mail($to,$subject,$message,$headers); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/#findComment-1242355 Share on other sites More sharing options...
IrOnMaSk Posted July 13, 2011 Author Share Posted July 13, 2011 thanks for replying decro2 and teynon. mysql_error() die (which is actually exit). oopse for got to take it out... I tested try and catch, but it's not working just gave me the unexpected "{" at the try line. looked at the manual, but found no example like so... Quote Link to comment https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/#findComment-1242379 Share on other sites More sharing options...
IrOnMaSk Posted July 14, 2011 Author Share Posted July 14, 2011 i got it to work... here's how to email alert if connection fail <?php $conn = @mysql_connect('localhost', 'root', 'wrongpass'); if (!$conn) { $to = [email protected]'; $subject = 'connection status'; $message = 'can\'t connect to the database'; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; //More headers $headers .= 'From: <[email protected]>' . "\r\n"; mail($to,$subject,$message,$headers); exit(); } $select = mysql_select_db('db'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/#findComment-1242773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.