S.M_Emamian
S.M_Emamian

Reputation: 17383

check $this->db->get() is empty or not

I'm using codeigniter framework.

how can I do check get function is empty or not?

...
$result = $this->db->get() 
 //how to check $result is empty or not ?

Upvotes: 1

Views: 4535

Answers (5)

Kaleem Ullah
Kaleem Ullah

Reputation: 7059

Better way according to Codeigniter Active records Documentation

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

    if($query->num_rows() > 0) {                   
        foreach ($query->result() as $row){
            echo $row->title;
        }               
    }
    else{
        //no record found.
        }

Upvotes: 1

Neil Marvin Enriquez
Neil Marvin Enriquez

Reputation: 19

$result = $this->db->get('myTable')->result();
print_r($result);die();

Upvotes: 0

ITit superpower
ITit superpower

Reputation: 526

$result = $this->db->get()->result_array();

Now check $result value its empty or not.

if(!empty($result)){
//code if not empty
}
else{
//code if empty
}

Upvotes: 0

jlocker
jlocker

Reputation: 1488

You can check like below,

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

if($result->num_rows() != 0){
  //you can do anything with data here
}
else{
  //empty
}

Upvotes: 0

Ruprit
Ruprit

Reputation: 743

You can do it this way

$query = $this->db->get();
if ($query->num_rows() > 0) {
  //record exists - hence fetch the row              
  $result = $query->row();               
} 
else
{
  //Record do not exists
}

Upvotes: 3

Related Questions