How to create a $variable from a mysql_error()

Hi Guys,
Is there a way to capture the results of a mysql_error()) into a variable such as $mysql_error = "???";?
I want to state what the error is in my own words.
Thanks,
Sas
mysql_query("UPDATE table SET column1=something") or die(mysql_error() );

Open in new window

LVL 1
sasnaktivAsked:
Who is Participating?
 
Ahmed MerghaniSoftware EngineerCommented:
Yes you can by checking error number instead of error msg.
mysql_query("UPDATE table SET column1=something");
$error_str = mysql_error();
$code = mysql_errno();
if( code != 0 ){
   // you can do your massage here based on the code number
}

Open in new window

This is mysql error numbers/codes reference:
http://dev.mysql.com/doc/refman/5.6/en/error-handling.html
0
 
Marco GasiFreelancerCommented:
First, you should use mysqli instead of mysql extension: mysql extension is deprecated and it will be no more supported.

Secondly, error() function in mysqli (or mysql_error() function in mysql) is useful to debug your script, in order to get the exact error infos. In production server is a security issue to show mysql and php messages to the users and you can safely replace them with another error message:

$error_msg = 'Sorry, this is my error message';
mysql_query("UPDATE table SET column1=something") or die($error_msg);

Open in new window

0
 
sasnaktivAuthor Commented:
Thanks, I can work with that code. It gives me the actual error message which allows me to address each error uniquely.
Have a good weekend,
Sas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.