Over a million developers have joined DZone.

Displaying the generated SQL from a Zend\Db\Sql object

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Couchbase.

If you use Zend\Db\Sql to generate your SQL, then it's useful to find out what the generated SQL looks like.

Consider code like this:

public function fetchAllWithTitleSince($title, $since)
    $sql = new Sql($this->dbAdapter);
    $select = $sql->select();
    $select->columns(array('id', 'title', 'url', 'date_updated'));
    $select->where->like('title', "%$title%");
    $select->where->greaterThanOrEqualTo('date_created', date('Y-m-d', strtotime($since)));
    $statement = $this->dbAdapter->createStatement();
    $select->prepareStatement($this->dbAdapter, $statement);
    return $statement->execute();

To find out what the generated SQL will look like, you can use the $select's getSqlString()method:


For me, this results in lots of warnings along the lines of:

Lots of notices

This is less than helpful, so to avoid the warnings, you need to supply the correct platform information to the method:


So we now get:

Expected SQL string

This is much better!

Are you a developer? Don’t miss the monthly Couchbase Developer Community Newsletter, covering the latest headlines on NoSQL and much more. Brought to you in partnership with Couchbase.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}