+ Reply to Thread
Results 1 to 5 of 5

Thread: Reporting SQL Errors with Zend_Db

  1. #1
    santouras is offline Junior Member
    Join Date
    Jun 2007
    Location
    perth, australia
    Posts
    11

    Default Reporting SQL Errors with Zend_Db

    Hi guys,

    Hopefully this isn't a really silly question... But I'm confused as to how to get Zend_Db to report on SQL errors. I know I can use the profiler, but when my application throws an error I would love to see "SQL Error... incorrect syntax...." etc etc. Do I need to use a try/catch block around every piece of SQL?

    In an semi related question, the profiler returns the SQL query in its unbound state, with ":variables" in place instead of their substituted values. Is this how PDO works or is there a way to see the variables that I've substituted in there.

  2. #2
    acraft is offline Junior Member
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    1

    Default

    When executing your queires you should put them in try catch blocks to catch any exceptions thrown and deal with them. You can put several queries in a try/catch block.

    I use the mysqli adapter and it has the same behaviour. The variables are not replaced till the query is prepared and the parameters sent to the server for processing.

    HTH

    Andrew

  3. #3
    santouras is offline Junior Member
    Join Date
    Jun 2007
    Location
    perth, australia
    Posts
    11

    Default

    thanks Andrew. Writing try/catch style is going to be a touch annoying around all the queries but if it must be done then it must be done I guess

  4. #4
    santouras is offline Junior Member
    Join Date
    Jun 2007
    Location
    perth, australia
    Posts
    11

    Default

    My final solution to this turned out quite nicely, and I hope this will be of some use to other people. I extended the DB adaptor for the db object I was using to contain automatic try/catch error reporting. Here's snippets of the relevant code

    [PHP]
    class NC_Db_Adaptor extends Zend_Db_Adapter_Pdo_Mysql {

    /**
    * Wrapper function for query function to allow for nicer debugging
    *
    * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
    * @param array $bind An array of data to bind to the placeholders.
    * @return Zend_Db_Pdo_Statement
    * @throws Exception To re-throw PDOException.
    */
    public function query($sql, $bind = array()) {
    try {
    return parent::query($sql, $bind);
    } catch (Exception $e) {
    //check if website is live or local. if live, email and supress error, if
    //local then spit out error message including the sql and bind params

    throw new Exception($e);
    }
    }
    }
    [/PHP]

    Hope some people find this useful

  5. #5
    jmut is offline Junior Member
    Join Date
    Jul 2007
    Posts
    2

    Default

    Quote Originally Posted by santouras View Post
    My final solution to this turned out quite nicely, and I hope this will be of some use to other people. I extended the DB adaptor for the db object I was using to contain automatic try/catch error reporting. Here's snippets of the relevant code

    [PHP]
    class NC_Db_Adaptor extends Zend_Db_Adapter_Pdo_Mysql {

    /**
    * Wrapper function for query function to allow for nicer debugging
    *
    * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
    * @param array $bind An array of data to bind to the placeholders.
    * @return Zend_Db_Pdo_Statement
    * @throws Exception To re-throw PDOException.
    */
    public function query($sql, $bind = array()) {
    try {
    return parent::query($sql, $bind);
    } catch (Exception $e) {
    //check if website is live or local. if live, email and supress error, if
    //local then spit out error message including the sql and bind params

    throw new Exception($e);
    }
    }
    }
    [/PHP]

    Hope some people find this useful
    Last line better be
    [php]
    throw $e;
    //not throw new Exception ($e);
    [/php]

    as in your case you loose whole exception trace and there is little to now use of such exception.

+ Reply to Thread

Similar Threads

  1. why i do not get the errors?
    By derrida in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 12-13-2009, 02:16 PM
  2. zf.sh throws errors on Mac
    By tixrus in forum Tooling and Rapid Application Development (RAD)
    Replies: 4
    Last Post: 12-11-2009, 05:14 AM
  3. Zend_Session_SaveHandler_DbTable Errors
    By clips401 in forum Authentication & Authorization
    Replies: 0
    Last Post: 08-12-2009, 05:56 PM
  4. quoteInto errors
    By MightyMouse in forum Databases
    Replies: 1
    Last Post: 10-03-2008, 02:11 AM
  5. Getting Errors using Zend_Filter_Input
    By O1eg in forum Core Infrastructure
    Replies: 0
    Last Post: 02-27-2008, 01:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts