Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2007, 01:08 AM
Junior Member
 
Join Date: Jun 2007
Location: perth, australia
Posts: 10
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-14-2007, 03:29 PM
Junior Member
 
Join Date: Jun 2007
Location: Canada
Posts: 1
Send a message via MSN to acraft
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-15-2007, 02:02 AM
Junior Member
 
Join Date: Jun 2007
Location: perth, australia
Posts: 10
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-05-2007, 07:06 AM
Junior Member
 
Join Date: Jun 2007
Location: perth, australia
Posts: 10
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 Code:
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);
  }
}

Hope some people find this useful
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-19-2007, 05:51 AM
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 Code:
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);
  }
}

Hope some people find this useful
Last line better be
PHP Code:
throw   $e
//not    throw new Exception ($e); 
as in your case you loose whole exception trace and there is little to now use of such exception.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:39 AM.