php中countresult,codeigniter中count_all_results()问题

SQL语句是这样的:

$this->db->select('*')->from('mytable');

$count=$this->db->count_all_results() ;

$config = array(

'base_url' => '/financing/dayconsumption',

'total_items' => $count,

'current_page' => $page,

'items_per_page' => $this->pagesize,

);

$this->pagination2->init($config);

$this->db->limit($this->pagesize, $this->pagination2->sql_offset);

$query = $this->db->get();

。。。。。。

运行后报错:

A Database Error Occurred

Error Number: 1096

No tables used

SELECT * LIMIT 20

Line Number: 330

调试后发现问题出在:$count=$this->db->count_all_results() ;这,查看一下count_all_results()的源码:

/**

* "Count All Results" query

*

* Generates a platform-specific query string that counts all records

* returned by an Active Record query.

*

* @paramstring

* @returnstring

*/

public function count_all_results($table = '')

{

if ($table != '')

{

$this->_track_aliases($table);

$this->from($table);

}

$sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));

$query = $this->query($sql);

$this->_reset_select(); //注意这里

if ($query->num_rows() == 0)

{

return 0;

}

$row = $query->row();

return (int) $row->numrows;

}

方法中调用了_reset_select()方法,继续追踪_reset_select()方法:

/**

* Resets the active record values. Called by the get() function

*

* @returnvoid

*/

protected function _reset_select()

{

$ar_reset_items = array(

'ar_select'=> array(),

'ar_from'=> array(),

'ar_join'=> array(),

'ar_where'=> array(),

'ar_like'=> array(),

'ar_groupby'=> array(),

'ar_having'=> array(),

'ar_orderby'=> array(),

'ar_wherein'=> array(),

'ar_aliased_tables'=> array(),

'ar_no_escape'=> array(),

'ar_distinct'=> FALSE,

'ar_limit'=> FALSE,

'ar_offset'=> FALSE,

'ar_order'=> FALSE,

);

$this->_reset_run($ar_reset_items);

}

其实到这里,看注释就能猜到了,active record被重置了。为了确认一下,继续追踪_reset_run(),就在_reset_select()上面:

/**

* Resets the active record values. Called by the get() function

*

* @paramarrayAn array of fields to reset

* @returnvoid

*/

protected function _reset_run($ar_reset_items)

{

foreach ($ar_reset_items as $item => $default_value)

{

if ( ! in_array($item, $this->ar_store_array))

{

$this->$item = $default_value;

}

}

}

果然,active record被重置了!!知道原因了,调用count_all_results()后,会将active record重置,后面的$query就获取不到数据了,解决办法:

$this->db->start_cache();

$this->db->select('*')->from('mytable');

$this->db->stop_cache();

......

$query = $this->db->get();

$items = $query->result();

$this->db->flush_cache();

以上就介绍了codeigniter中count_all_results()问题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值