Zend DB Framework examine query for an update

Chris C picture Chris C · Jun 17, 2009 · Viewed 32.5k times · Source

So you can use something like this:

$query = $db->select();
$query->from('pages', array('url'));
echo $query->__toString();

to examine the sql that the Zend Db Framework is going to use for that SELECT query. Is there an equivilent way to view the SQL for an update?

$data = array(
   'content'      => stripslashes(htmlspecialchars_decode($content))
);      
$n = $db->update('pages', $data, "url = '".$content."'");
??

Answer

Bill Karwin picture Bill Karwin · Jun 17, 2009

Use Zend_Db_Profiler to capture and report SQL statements:

$db->getProfiler()->setEnabled(true);
$db->update( ... );
print $db->getProfiler()->getLastQueryProfile()->getQuery();
print_r($db->getProfiler()->getLastQueryProfile()->getQueryParams());
$db->getProfiler()->setEnabled(false);

Remember to turn the profiler off if you don't need it! I talked to one fellow who thought he had a memory leak, but it was the profiler instantiating a few PHP objects for each of the millions of SQL queries he was running.

PS: You should use quoteInto() in that query:

$n = $db->update('pages', $data, $db->quoteInto("url = ?", $content));