Welcome Guest, Not a member yet? Register   Sign In
Query help with searching for users
#1

[eluser]JamieBarton[/eluser]
Hi guys,

I'm after a little help on this query I'm trying to run.

I'm having one of those days.

The code below is what I'm having trouble with. I've commented out what I know doesn't work because I know I need to use brackets in this query.

Basically I want to search for rows in the 'users' table WHERE username LIKE $name or name LIKE $name AND exclude myself from the query by saying my 'id' is $user_id.

Code:
public function search_for_users($term) {
  //$this->db->like('username', $term);
  //$this->db->or_like('name', $term);
  
  //$this->db->where('username !=', current_user()->username);
  
  $user_id = current_user()->id;
  
  $where = "(`username` LIKE $term OR `name` LIKE $term) AND `id` != $user_id";
  
  $this->db->where($where);
  
  return $this->db->get($this->_users_table);
}

Hopefully one of you will be able to help!


Regards,
Jamie
#2

[eluser]Aken[/eluser]
You'll likely need to write the query by hand for this. There's no good way for the query builder to section off conditionals in parentheses.

Remember, you don't need to use the query builder if your application isn't supporting multiple DB environments. Sure it's handy for creating simpler queries, but for complex you should stick with writing them by hand.




Theme © iAndrew 2016 - Forum software by © MyBB