PhpCoder
PhpCoder

Reputation: 427

How to write the given query in CodeIgniter

I want to write the given query in codeigniter , how can I do so?

SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);

I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,

$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();

it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.

Upvotes: 0

Views: 85

Answers (3)

Vinie
Vinie

Reputation: 2993

Use $this->db->query();

$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();

Upvotes: 1

Sundar
Sundar

Reputation: 4650

You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE

<?php 
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();

Upvotes: 1

Suvash sarker
Suvash sarker

Reputation: 3170

You can use this simple way $this->db->query('your query');

Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter

Upvotes: 1

Related Questions