views:

208

answers:

2

Hi, 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?

+3  A: 

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.

karim79
A: 

If you already have the select object (means the from() was called before), you should use $select->reset(Zend_Db_Select::COLUMNS); and then call columns() as you do in the example.

Tomáš Fejfar