Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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.
mysql_query("UPDATE table SET column1=something") or die(mysql_error() );

Open in new window

1 Solution
Ahmed MerghaniCommented:
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:
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

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,

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now