How to create a $variable from a mysql_error()

Posted on 2014-08-16
Last Modified: 2014-08-16
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

Question by:sasnaktiv
    LVL 8

    Accepted Solution

    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:
    LVL 30

    Expert Comment

    by:Marco Gasi
    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

    LVL 1

    Author Closing Comment

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now