PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Executing Select Queries

This section describes how to execute the query represented by a Zend_Db_Select object.

Executing Select Queries from the Db Adapter

You can execute the query represented by the Zend_Db_Select object by passing it as the first argument to the query() method of a Zend_Db_Adapter_Abstract object. Use the Zend_Db_Select objects instead of a string query.

The query() method returns an object of type Zend_Db_Statement or PDOStatement, depending on the adapter type.

Example 260. Example using the Db adapter's query() method

<?php
$select 
$db->select()
             ->
from('products');

$stmt $db->query($select);
$result $stmt->fetchAll();

Executing Select Queries from the Object

As an alternative to using the query() method of the adapter object, you can use the query() method of the Zend_Db_Select object. Both methods return an object of type Zend_Db_Statement or PDOStatement, depending on the adapter type.

Example 261. Example using the Select object's query method

<?php
$select 
$db->select()
             ->
from('products');

$stmt $select->query();
$result $stmt->fetchAll();

Converting a Select Object to a SQL String

If you need access to a string representation of the SQL query corresponding to the Zend_Db_Select object, use the __toString() method.

Example 262. Example of the __toString() method

<?php
$select 
$db->select()
             ->
from('products');

$sql $select->__toString();
echo 
"$sql\n";

// The output is the string:
//   SELECT * FROM "products"

Zend Framework