Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cakephp 2.5.4: How to display proper error messages from mysql response

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 136
    Comment on it

    There are conditions when we need to show sql error messages to users such as Duplicate Entries from database.

    For example in cakephp, if there is duplicacy error from database then it will show following message to the user:

    "Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry"

    To show proper message instead of this then you need to display information and use a view. To pass information from the controller to the view use the set method. In order to do so write the following code:

     

    $this->set('errormsg', $e->getMessage());

     

    If you want to use AJAX you can set it like this:

     

     $this->set('_serialize', array('errormsg'));

     

    Thanks for reading the blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: