Welcome Guest, Not a member yet? Register   Sign In
composing sql command
#1

[eluser]Unknown[/eluser]
Hi guys,
I need to write this sql in CI:

Code:
SELECT * FROM product WHERE
`producer_id` = '1' AND
(`name` LIKE '%q%' OR `content` LIKE '%q%' )
ORDER BY `name` asc LIMIT 2



as you see, I have data from search-form with 2 variables - producer_id and q (some searched value).

If I use this code, I get sql command without brackets (and that´s wrong):
Code:
$this->db->where('producer_id', $search['producer_id']);
$this->db->or_where('name LIKE', $search['q']);
$this->db->or_where('content LIKE', $search['q']);

$this->db->order_by('name', 'asc')->get('product', $limit, $offset)->result_array();

Code:
SELECT * FROM product WHERE
`producer_id` = '1' AND
`name` LIKE '%q%' OR `content` LIKE '%q%'
ORDER BY `name` asc LIMIT 2

do you have any suggestions how to do this?
thank you
#2

[eluser]rwestergren[/eluser]
You could do this two ways:

1: A custom string, the active record way

Code:
$this->db->where('producer_id', $search['producer_id']);
$this->db->where("(`name` LIKE '%$search[q]%' OR `content` LIKE '%$search[q]%' )", NULL, FALSE);
$this->db->order_by('name', 'asc')->get('product', $limit, $offset)->result_array();

2: Using your intended query manually

Code:
$results = $this->db->query("
     SELECT * FROM product WHERE
     `producer_id` = '1' AND
     (`name` LIKE '%$search[q]%' OR `content` LIKE '%$search[q]%' )
     ORDER BY `name` asc LIMIT 2
     ")->result_array();
#3

[eluser]Unknown[/eluser]
thank you,




Theme © iAndrew 2016 - Forum software by © MyBB