Reputation: 92621
I am attempting to build a query using code igniter's active records class that looks like this:
SELECT * from contacts where account = 1 and (fname like 'k%' or lname like 'k%')
however I am not sure how to use the functions to do this.
and ideas?
Upvotes: 0
Views: 2178
Reputation: 31
You can use query grouping from CI 3.0
$this->db->select('*')->from('contacts ')
->group_start()
->like('fname', 'k', 'after');
->or_like('lname', 'k', 'after');
->group_end()
->where('account', 1)
->get();
Upvotes: 0
Reputation: 11
Instead of using normal query, you can use the suggestion from the ellislab forum https://ellislab.com/forums/viewthread/185983/ Instead of using
$this->db->like('location', $your_string);
use:
$this->db->where('location LIKE', '%'.$your_string.'%');
and or_where instead of or_like.
Upvotes: 0
Reputation: 622
Yo can allways run it as a normal query:
$sql = "SELECT * from contacts where account = 1 and (fname like ? or lname like ?)";
$query = $this->db->query($sql, array( $like1, $like2));
I use more often this than the active record class as I'm keen in writing sql statements.
Upvotes: 0
Reputation: 1691
You can also use the like query
$this->db->like('title', 'match');
// Produces: WHERE title LIKE '%match%'
And or_like
$this->db->like('title', 'match');
$this->db->or_like('body', $match);
Upvotes: 1
Reputation: 62392
$this->db->where('account', '1');
$this->db->where(' (fname LIKE "k%" OR lname LIKE "k%" ) ');
$query = $this->db->get('contacts');
Then just deal with $query
like a normal Codeigniter database result.
Upvotes: 3