开发者

fetchAll statement to return only some columns

I am developing a web app using zend framework in which I want to enable my users to search from other users using zend fetchAll function. But its returning complete column and I want several values only. Here is my sample code:

$query=$table->select('id')->where("name LIKE ?",'%'.$str.'%')->limit(10);
$model=new Application_Mod开发者_开发百科el_Users();
$rowset=$model->getDbTable()->fetchAll($query)->toArray();

I want to get only name and some other columns. Its returning all columns including password too.:p


$select = $db->select();

$select->from('some_tbl', array('username' => 'name')); // SELECT name AS username FROM some_tbl
$select->from('some_tbl', 'name');                      // SELECT name FROM some_tbl
$select->from('some_tbl', array('name'));               // SELECT name FROM some_tbl

$select->from('some_tbl', '*'); // SELECT * FROM some_tbl
$select->from('some_tbl');      // SELECT * FROM some_tbl

// in case of joins, to disable column selection for a table
$select->from('some_tbl', array()); 


You can add a columns call to fetch only specific columns. In this case, we request the id and name columns:

$columns = array('id', 'name');
$query = $table->select()->columns($columns)->where("name LIKE ?",'%'.$str.'%')->limit(10);


// Create the Zend_Db_Select object
$select = $db->select();

// Add a FROM clause
$select->from( ...specify table and columns... )

// Add a WHERE clause
$select->where( ...specify search criteria... )

// Add an ORDER BY clause
$select->order( ...specify sorting criteria... );

Building Select queries - Zend Framework programming documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜