stig1 Posted April 7, 2009 Share Posted April 7, 2009 I would like to put error messages from mysql or my odbc connection into variables to which later I will email them out (already know how to use mail, so no help needed there) How would I put the following into a variable called $error should the sql fail in anyway. $iItemSQL = "insert into items (item, type1, type2, desc1, createdate, lastupdate) values ('".$item."', '".$type1."', '".$type2."', '".$desc1.'", '".$createdate."', '".$lastupdate."')"; $iItems = mysql_query($iItemSQL) or die (mysql_error()); Running the above script, if it was to fail would just die and display the error on the screen. Instead of just die and display error, I would like the script to put that error message into a variable to which i can later call, and the script can continue to run. Would this possible work? if(!$iItems){ $error = Error: '.mysql_errno().' '.mysql_error(); } Help is always apprecaited and a huge thankyou in advance. Link to comment https://forums.phpfreaks.com/topic/152951-error-messages-into-variable/ Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 if (mysql_query($query)) { echo 'It worked'; } else { echo 'It failed!'; $error=mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/152951-error-messages-into-variable/#findComment-803283 Share on other sites More sharing options...
JasonLewis Posted April 7, 2009 Share Posted April 7, 2009 You can also just do: mysql_query("SELECT * FROM table") or $error = mysql_error(); Of course you could jazz it up a bit. Link to comment https://forums.phpfreaks.com/topic/152951-error-messages-into-variable/#findComment-803284 Share on other sites More sharing options...
stig1 Posted April 7, 2009 Author Share Posted April 7, 2009 I plan to jazz it up abit, but i just had no idea how to get it into a variable. Once its in a variable I can add text, and etc to make it more user friendly. Would my suggested answer work? Link to comment https://forums.phpfreaks.com/topic/152951-error-messages-into-variable/#findComment-803285 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 The "or die" bit would kick in if the query failed so you'd have to remove that but other than that - yes, it should work. Link to comment https://forums.phpfreaks.com/topic/152951-error-messages-into-variable/#findComment-803287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.