fetchAll语句只返回一些列

时间:2011-09-22 09:23:13

标签: php mysql zend-framework

我正在使用zend框架开发一个Web应用程序,我想让我的用户使用zend fetchAll函数从其他用户搜索。但它返回完整列,我只想要几个值。这是我的示例代码:

$query=$table->select('id')->where("name LIKE ?",'%'.$str.'%')->limit(10);
$model=new Application_Model_Users();
$rowset=$model->getDbTable()->fetchAll($query)->toArray();

我想只获得名称和其他一些列。它返回包括密码在内的所有列。:p

3 个答案:

答案 0 :(得分:8)

$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()); 

答案 1 :(得分:4)

您可以添加columns调用以仅提取特定列。在这种情况下,我们请求id和name列:

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

答案 2 :(得分:1)

// 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