: This article describes count_all_results () in codeigniter. For more information about PHP tutorials, see. The SQL statement is as follows:
$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();
......
An error is reported after running:
A Database Error Occurred
Error Number: 1096
No tables used
SELECT * LIMIT 20
Line Number: 330
After debugging, the following problems are found:$count = $this->db->count_all_results() ;Check the count_all_results () source code:
/*** "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 (); // note here if ($ query-> num_rows () = 0) {return 0 ;}$ row = $ query-> row (); return (int) $ row-> numrows ;}
The _ reset_select () method is called in the method to continue tracing the _ reset_select () method:
/** * 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);}
As a matter of fact, you can guess from the comment. The active record is reset. To confirm, continue tracing _ reset_run () on _ 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;}}}
Sure enough, the active record is reset !! Knowing the cause, after count_all_results () is called, the active record will be reset, and the subsequent $ query will not be able to get the data. solution:
$this->db->start_cache();$this->db->select('*')->from('mytable');$this->db->stop_cache();......$query = $this->db->get();$items = $query->result();$this->db->flush_cache();
The above introduces the count_all_results () problem in codeigniter, including some content, and hopes to help friends who are interested in PHP tutorials.