user3141441
user3141441

Reputation: 31

How to fetch the data from db in codeigniter

$sql='SELECT * FROM PRODUCTS WHERE CATEGORY=array['0'] && CATEGORY=array['1'] && CATEGORY=array['2']';

my problem is array size was dependent sometimes it's count was 10 and some times it was 1. if it was 10 i have to write the in query up to 10 times. how to over come this. finally i'm using codeigniter in that how to write this query.

Upvotes: 0

Views: 106

Answers (4)

Agha Umair Ahmed
Agha Umair Ahmed

Reputation: 1020

You have to query like that

$this->db->where_in('CATEGORY', $array);
$executequery = $this->db->get('PRODUCTS');

Upvotes: 0

Try to format your query like this

$this->db->select('*');
$this->db->where_in('CATEGORY', $array);
$query = $this->db->get('mytable');

Upvotes: 1

zerokavn
zerokavn

Reputation: 1333

This is an example in CI

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);

Upvotes: 0

Kumar V
Kumar V

Reputation: 8838

try this code:

$cat_ids = implode(",",$array);

$sql = "SELECT * FROM PRODUCTS WHERE CATEGORY in (".$cat_ids.")";

Upvotes: 0

Related Questions