This section describes how to execute the query represented by a
Zend_Db_Select object.
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 254. Example using the Db adapter's query() method
<?php
$select = $db->select()
->from('products');
$stmt = $db->query($select);
$result = $stmt->fetchAll();
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 255. Example using the Select object's query method
<?php
$select = $db->select()
->from('products');
$stmt = $select->query();
$result = $stmt->fetchAll();
If you need access to a string representation of the SQL query
corresponding to the Zend_Db_Select object, use the
__toString() method.
Example 256. 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"




