Php 数据库中的记录计数
我用 创建查询。现在我想做一个类似的函数来计算查询中的记录Php 数据库中的记录计数,php,kohana,Php,Kohana,我用 创建查询。现在我想做一个类似的函数来计算查询中的记录 public function get_all_news() { $result = DB::select() ->from("parys_news") ->order_by("ID", "DESC") ->execute() ; return $result; } 如何在Kohana中执行此操作?在特定
public function get_all_news()
{
$result = DB::select()
->from("parys_news")
->order_by("ID", "DESC")
->execute()
;
return $result;
}
如何在Kohana中执行此操作?在特定情况下,要获取所有新闻计数的计数,只需调用返回结果对象上的方法即可,例如:
public function count_records($query)
{
}
如果您想要一个通用的count\u records($query)
方法,那么根据您想要传递的参数:
- 如果是简单的SQL查询字符串:
$news = YourModel->get_all_news(); $news_count = $news->count();
- 如果是数据库查询
public function count_records($query) { return DB::query(Database::SELECT, $query)->execute()->count(); }
- 或者它已经是Database_Result对象,那么只需:
public function count_records(Database_Query $query) { return $query->execute()->count(); }
public function count_records(Database_Result $result)
{
return $result->count();
}