I have the following code

$result = $handle->select()->from('store_details')
                               ->where('store_details.store_id=?', $id)
                               ->columns('store_details.store_name');
                               //->query(ZEND_DB::FETCH_OBJ);

However, when I run it, the entire row is selected, not just the column I wanted. Here is the output from __toString

SELECT `store_details`.*, `store_details`.`store_name` 
FROM `store_details` WHERE (store_details.store_id=8)

Any help?

Accepted Answer

The columns() method is for adding columns to an existing from or join. The correct way to build your query is:

$result = $handle->select()->from('store_details','store_details.store_name')->where('store_details.store_id=?', $id);

You need to specify the columns you want as the second parameter to the from() method, as a string if it is just one column, or as an array for multiple columns. From the Zend_Db_Select docs:

In the second argument of the from() method, you can specify the columns to select from the respective table. If you specify no columns, the default is "*", the SQL wildcard for "all columns".

You can list the columns in a simple array of strings, or as an associative mapping of column alias to column name. If you only have one column to query, and you don't need to specify a column alias, you can list it as a plain string instead of an array.

Written by karim79
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki