Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A better mysql_connect

Status
Not open for further replies.

Hokey

MIS
Oct 28, 2000
178
US
I am tring to change my current MySQL connections from the standard:
mysql_connect(localhost,username,passowrd) or die ("some error message");
To something a little more convient for tracking down problems on my site. This is what I have come up with

mysql_connect(localhost,username,passowrd) or send_error("databse connection, problem", mysql_error(), "index.php");

send_error() is a function in my library set that takes in multiple variables and emails me the information sent to it.

function send_error($subject,$message,$redirect){
mail("mhawks@carroll.edu",$subject,$message);
header ("Location: $redirect");
exit();
}

The send_error() function is being called, and the email is going out with the right subject line, and the user is being redirected properly, but the body of the email is not containing any informaion. I am tring to pass the mysql_error() inforation into it, but it dosent seem to work ... anyone see an obvious reason why this shouldnt work, or if there is a better way to accomplish what my basic goals are?

what other options to the "or die("message")" do we have on mysql_connect?
 
After a night of reading I think I solved my problem and figured that I would post it here in case someone else has a use for it.

It appears that the mysql_error() function does not return anything unless a MySQL connection is already active. Since the one of the failures that I want reported is the actual connection (mysql_connect()), no error message is returned. However, if you turn track_errors on in the php.ini, the variable $php_errormsg keeps the last error message.


example of code:
// send_error function in my php library
function send_error($subject,$message,$redirect){
mail("weberror@mydomain.com",$subject,$message);
header ("Location: $redirect");
exit();
}


@mysql_connect(localhost,username,password)
or send_error("Web error: Database connection",$php_errormsg,"dberror.php");


The send_error function will send an email with with the subject "Web error: Database connection" and the php error message in the body. The user will also be redirected to a web page that lets them know about the error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top