I'm using CodeIgniter's db class to create the query.
The query ends up looking like this;
SELECT `user_id`, `firstname`, `lastname`, `email`, `school_id`, `country_id`
FROM (`users`)
WHERE `user_id` != '1'
AND `firstname` = 'thomas'
OR `lastname` = 'thomas'
OR `email` = 'thomas'
OR `firstname` = 'jefferson'
OR `lastname` = 'jefferson'
OR `email` = 'jefferson'
In this case a user has searched for "thomas jefferson" and the user's own id is 1. So to make sure the user can't search for himself it checks so that the user_id isn't 1.
I know the query isn't that good but it works perfectly for what I need it for, as well as me being too lazy to do it correctly.
The PHP-code behind this all is
$this->db->select('user_id, firstname, lastname, email, school_id, country_id')->from('users');
$this->db->where('user_id !=', $this->session->userdata('user_id'));
for ($i = 0; $i < count($searchQuery); $i++) {
if ($i == 0) {
$this->db->where('firstname', $searchQuery[$i])->or_where('lastname', $searchQuery[$i])->or_where('email', $searchQuery[$i]);
} else {
$this->db->or_where('firstname', $searchQuery[$i])->or_where('lastname', $searchQuery[$i])->or_where('email', $searchQuery[$i]);
}
}
$this->db->limit(10);
$query = $this->db->get();
I'm 99% positive that the
WHERE `user_id` != '1'
AND(<-- this) `firstname` = ...
is wrong but I can't seem to figure out a different way to do it.