Page 1 of 1

Passing MySQL Error Message

Posted: Thu Aug 26, 2004 5:18 pm
by mrvanjohnson
Hope someone can help point me in the right direction. I have a online form that people fill out. I have the information writing to a database and then emails some administrators.

Basically what I would like is to past a message to the email if the database input fails. So the email still goes out with all the information plus a note saying it wasn't able add the information to the database.

So, obviously I don't want to do a

Code: Select all

<?php
mysql_query($sql) or die
?>
Been trying to do this

Code: Select all

<?php
mysql_query($sql) or define ("ERROR_MESSAGE", "Could not insert information into database - ".mysql_error()."\r\n");
?>
But I can't seam to figure out how to pass the ERROR_MESSAGE to the body of the email. Any help would be greatly appreciated.

Posted: Thu Aug 26, 2004 5:22 pm
by feyd

Code: Select all

mail('foo','error','message' . (defined('ERROR_MESSAGE') ? constant('ERROR_MESSAGE') : ''),'headers');

Posted: Thu Aug 26, 2004 5:24 pm
by Illusionist
create your own function

Code: Select all

mysql_query($sql) or dieError("Could not add information into database - ".mysql_error()."<br>");
.....
......
function dieError($msg){
   $mail_mesasge .= $msg
}
Something like that
EDIT: Or what feyd said!

Posted: Thu Aug 26, 2004 5:28 pm
by mrvanjohnson
God 4 minutes and a couple of great responses, I love the f**king forum .. BEST ON THE NET PERIOD

Thanks feyd. I actually want to display a successful insert message if everything works and if not dispay the bad message but I think you've pointed me in the right direction with the defined command. I'll see if I can work through it. Thanks again.

Posted: Fri Aug 27, 2004 9:29 am
by pickle
Use [php_man]mysql_affected_rows[/php_man](). If the insert is successful, it will return 1. If not, it will return 0.

Posted: Fri Aug 27, 2004 11:18 am
by mrvanjohnson
I thought about using the mysql_affected rows() but if it failed I wanted to past the reason why it failed into the email so that it could then be forwarded to me.

I ended up writting a if defined statment that worked like a charm. Thanks again feyd