Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:01:56+00:00 2026-06-17T16:01:56+00:00

I am trying to get up and running with stored procedures in Zend Framework

  • 0

I am trying to get up and running with stored procedures in Zend Framework 2. I tried to return an error code with an out parameter in the stored procedure, but I have been unable to make that work. Then I thought that when an error occurred, I could just catch an exception in PHP. The problem is that I can’t seem to get access to the specific error code – only a general one (e.g. 23000 – integrity constraint violation). Here is an example of what I want to do (or similar):

try {
    $result = $this->dbAdapter->query('CALL sp_register_user(?, ?)', array('username', 'password'));
}

catch (\Exception $e) {
    switch ($e->getCode()) {
        case 1062:
            // Duplicate entry
            break;

        case 1452:
            // Cannot add or update a child row
            break;
    }
}

That is, I would like to be able to check exactly which error occurred. The problem, though, is that the exception that is thrown has an error code of 23000 and not one of the above.

An InvalidQueryException is thrown in Zend\Db\Adapter\Driver\Pdo\Statement on line 220:

try {
    $this->resource->execute();
} catch (\PDOException $e) {
    throw new Exception\InvalidQueryException('Statement could not be executed', null, $e);
}

The PDOException here contains an error code of 23000 in my case. The null parameter is the error code. So in my catch block, I will actually be catching an InvalidQueryException with an error code of 0, which is not all that useful. This exception does provide me access to previous exceptions (the last parameter above), which would be the PDOException, like this:

// try block omitted
catch (InvalidQueryException $e) {
    $previous_exception_error_code = $e->getPrevious()->getCode();
}

On duplicate entry (1062), the error code is 23000. For a “cannot add or update a child row” (1452), it would also be 23000. Therefore I am not sure how I can distinguish between them in my application. What if I wanted to present different error messages to the user for the two errors? The exception message is: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user' for key 'username'. This is a string, though, so searching for the error code would just be too hacky. I am hoping to be able to still abstract away from the fact that I am using PDO as the concrete implementation.

In the end, this seems more about PDO and MySQL than ZF2. I am not really sure where to go from here, so any ideas on how I can distinguish between the error codes would be much appreciated. That is, to know when an error 1062 or 1452 occurred rather than just 23000.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:01:57+00:00Added an answer on June 17, 2026 at 4:01 pm

    You want to switch on $e->errorInfo[1], which is the driver-specific error code (instead of the standardised SQLSTATE value).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get this running: JAXB interface But I always get the error
I am trying to get current running context in android, I tried to use:
I'm trying to learn how to use MySQL stored procedures. MySQL accepted my procedure:
When running a stored procedure, we're getting the error 297 The user does not
I'm trying to implement optional parameters in a stored procedure that I have but
I have a simple rails 2.3.4 application I am trying to get running with
I'm trying to get rails running on my mac which is running lion. I'm
I'm trying to get Django running on GAE using this tutorial . When I
I'm trying to get perl running under on my Apache 2 macports install. I'm
I am just trying to get Hadoop running on my laptop running 64-bit Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.