如何计算 Zend Framework 2 中的行数 [英] How to count number of rows within Zend Framework 2
问题描述
我需要计算 MySql 查询的结果行数.在这里,我将 TableGateway
类扩展到我的类,这是我的代码.
I need to count result rows of MySql query. here I extended TableGateway
class to my class this is my code.
public function get_num_of_rows(){
$sql = 'SELECT count(q_no) FROM questions';
//code ????????????????
$result = $this->select();
return $result;
}
那么我如何执行 SELECT count(q_no) FROM questions
?
推荐答案
无需编写自己的 sql 查询.当你执行 $this->select() 时,你会得到 Zend\Db\ResultSet\ResultSet 的一个实例.ResultSet 有方法计数.
There is no need to write own sql query. When you do $this->select(), you get an instance of Zend\Db\ResultSet\ResultSet. ResultSet has method count.
$result = $this->select();
return $result->count();
但不要忘记添加 'options' =>array('buffer_results' => true)
到您的数据库适配器.
But do not forget to add 'options' => array('buffer_results' => true)
to your DB adapter.
更新:
这是我在某处写过的最愚蠢的事情.总是从只需要资源的数据返回.在这里你只需要 1 个标量.所以https://stackoverflow.com/a/13810175/1353837是正确的.
It is the stupidest thing I've ever written somewhere. Always return from resource required only data. Here you need 1 scalar only. So https://stackoverflow.com/a/13810175/1353837 is correct.
这篇关于如何计算 Zend Framework 2 中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!