简体   繁体   中英

CodeIgniter Like and Where

I have a filter status and search, I am trying to make a query with this logic

If i have an input with both filter status and search

select * from table_name where status = 'Active' AND name like %search% or address like %search%

I want to search through the filtered record using status and vice versa, I want to status filter the searched records

I am trying to use this query but it seem not the right query

$this->db->select('*')->from('tbl_users');
$this->db->like('name', $string, "both");
$this->db->or_like('address', $string, "both");
$this->db->where('status', $status);
$this->db->get();

You need to specify some groups:

$this->db->select('*')->from('tbl_users')
    ->group_start()
        ->where('status', $status)
        ->or_group_start()
            ->like('name', $string, 'both')
            ->like('address', $string, 'both')
        ->group_end()
    ->group_end()
->get();

See https://codeigniter.com/user_guide/database/query_builder.html#query-grouping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM