Count_all_results () Problem in CodeIgniter

Source: Internet
Author: User
The SQL statement is this:

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

。。。。。。

Error after Operation:

A Database Error occurred

Error number:1096

No tables used

SELECT * LIMIT 20

Line number:330

After debugging found that the problem is: $count = $this->db->count_all_results() ; This, look at the source of Count_all_results ():

        /** * "Count all Results" Query * * generates a platform-specific query string this counts all records * returned by an Ac tive Record Query. * * @paramstring * @returnstring */public function count_all_results ($table = ") {if ($table! =") {$this->_track_alias ES ($table); $this->from ($table);} $sql = $this->_compile_select ($this->_count_string $this->_protect_identifiers (' numrows ')); $query = $ This->query ($sql); $this->_reset_select ();  Notice 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);}

In fact, by looking at the annotations, we can guess that the active record has been reset. To confirm, continue to track _reset_run (), just above _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 was reset!! Knowing the reason, after calling Count_all_results (), the active record will be reset, the following $query will not get the data, the workaround:

$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 describes the CodeIgniter in the Count_all_results () problem, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.