Joy
Joy

Reputation: 4483

How to know if any row of a table satisfies a condition

I am executing a CodeIgniter statement to select a row from a table with the following query:

$result = $this->db->query('select * from table_name where cond1 and cond2);

But how will I know any row is selected or not. I have used count($result) to determine the number of rows selected. But it is returning 1 in both the cases i.e. when a row satisfies the conditions and when no row does not satisfy the condition. So I am asking whether there is any way to check that. Thank you.

Here assume that there can be only one row present with satisfying cond1 and cond2.

Upvotes: 0

Views: 343

Answers (5)

whispersan
whispersan

Reputation: 1039

It would be useful to see how your full query is written, put in two where clauses like this and it binds them with AND:

           $this->db
           ->select ('*')
           ->from('table_name')
           ->where('table_name.column', cond1)
           ->where('table_name.column2', cond2);

$result = $this->db->get();
if ($result) {
//this means you have results, do something with them here
}

$count = $result->num_rows();

OR

$count = count($result->result());

Upvotes: 0

vistur
vistur

Reputation: 631

if($result->num_rows()>0){
    //One or more rows satisfies the condition
}else {
    //Zero rows satisfies the condition
}

Upvotes: 1

DevZer0
DevZer0

Reputation: 13535

Based on the documentation the correct answer is echo $result->num_rows;

Upvotes: 0

dar7yl
dar7yl

Reputation: 3747

$result = $this->db->query('select count(*) from table_name where cond1 and cond2');

then you need to extract the count from the first column of the returned row.

Upvotes: 0

M Shahzad Khan
M Shahzad Khan

Reputation: 935

You looking for this?

$result ->num_rows();

Upvotes: 0

Related Questions