Search code examples
phpzend-frameworkzend-db

ZendFramework - How to get the database insert error and stop auto redirection to error page?


Trying to insert row, and it fails to insert because of duplicate key found. And throws to error page. But how do i avoid going to error page but simply get the error result? so that i can echo it.

$db->insert("university", $data);
$lastID = $db->lastInsertId();
# when it fails to insert
# how can i run this echo
echo $theCauseOfErrorOnlyDoNotRedirectToError; //??

Solution

  • you should use a try catch block

    try {
        $db->insert("university", $data);
        $lastID = $db->lastInsertId();
    } catch(Exception $e) {
       // when it fails to insert
       // how can i run this echo
       echo $theCauseOfErrorOnlyDoNotRedirectToError; //??
    }
    

    You can review the documentation about exceptions and exception handling.