Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array to where in Codeigniter Active Record

I have a table in my database with adminId and clientId

There might be 20 records with the adminId of the logged in user and I'm trying to pull a list of clients.

I am wondering if there is a way i can say something like:

$this->db->where('id', '20 || 15 || 22 || 46 || 86'); 

I'm trying to do this with dynamic data (you never know how many clients Id's you'll need to pull). Any ideas?

like image 911
Daniel White Avatar asked Dec 05 '12 06:12

Daniel White


People also ask

Where is active record in CodeIgniter?

Active Record ClassCodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

How do I print a query in CI?

Use the below query to display the query string: print_r($this->db->last_query()); To display the query result follow this: print_r($query);


2 Answers

$this->db->where_in('id', ['20','15','22','42','86']); 

Reference: where_in

like image 150
joHN Avatar answered Sep 25 '22 06:09

joHN


Use where_in()

$ids = array('20', '15', '22', '46', '86'); $this->db->where_in('id', $ids ); 
like image 37
Stanley Avatar answered Sep 22 '22 06:09

Stanley