Passing MySQL Error Message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Passing MySQL Error Message

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

mail('foo','error','message' . (defined('ERROR_MESSAGE') ? constant('ERROR_MESSAGE') : ''),'headers');
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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
Post Reply