gloriaside
gloriaside

Reputation: 51

MySQL Count rows From Table WHERE userid equals? - CodeIgniter

I am having trouble with MySQL in CodeIgniter, I have the 3 columns:

ID  |  USERID  |  NAME    |  MOBILE
1        1         JAMES      55
2        1         JOHN       66
3        2         ANNE       33

I want to count the number of rows where the USERID is 1 in CodeIgniter, anyone can help me?

I expect the output will be 2, because there is 2 records assigned to the USERID 1.

Thanks

Upvotes: 3

Views: 7943

Answers (4)

Dilpreet Singh Sandhu
Dilpreet Singh Sandhu

Reputation: 26

CodeIgniter Model

function count($userid){
    $this->db->select('*');
    $this->db->from('table_name');
    $this->db->where('userid',$userid);
   return  $this->db->get()->num_rows();
}

Upvotes: 0

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

$this->db->where('USERID',1);
$this->db->from('my_table');
echo $this->db->count_all_results();

Upvotes: 4

SRIRAM
SRIRAM

Reputation: 1888

try this

select count(userid) from table where userid=1

Upvotes: 0

Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

Try this

$query = $this->db->query('SELECT * FROM my_table where USERID = 1');
echo $query->num_rows(); 

Upvotes: -1

Related Questions