Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/241915-email-alert-if-connection-fail/
Share on other sites

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();
}
?>

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...

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');

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.