chris chris - 5 months ago 22
SQL Question

PHP Codeigniter Database Helper Class "No tables used"

Ok, this isn't the full code obviously but this is the code up to where the error is.

$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
if($this->db->count_all_results() > 0)
{
$dataDB = $this->db->get();


I am getting "No table used" in regards to the
get()
statement of the final line I show here. I am still kind of new to the whole CI database class/helper, as I used to be more about handwriting full queriers. But I am trying to school myself on ORM layers and get better familiar with them. That being a mute point to my question however. Im trying to figure out what I did wrong with this statement, what is the fix, and if at all possible an explanation of how I did it wrong so I can understand and not to it again.

Answer

According to the documentation:

Normally, when an Active Record call is completed, all stored information is reset for the next call.

If you want to reuse the query, use the caching methods which can be found at the bottom of the same page. Don't forget to flush the cache when done:

$this->db->start_cache();
$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
$this->db->stop_cache();

if($this->db->count_all_results() > 0) {
     $dataDB = $this->db->get();
}
$this->db->flush_cache();

EDIT:

Another option where you use a single query instead of two:

$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
$query = $this->db->get();

if($query->num_rows() > 0) {
     $dataDB = $query->result();
}
Comments