Freelance Web Development Resource

web development resources – dolrich fortich

RSS
people

Grouping of WHERE statement using CodeIniter Active Record

To generate this SQL query using CodeIgniter Active Record.

SELECT `id` FROM (`users`)
		WHERE
			(first_name = 'test1' AND age =18) OR
			(last_name = 'test2' AND age =17)

You can do it like this.

$first_name = 'test1';
$last_name = 'test2';
$age1 = 18;
$age2 = 17;

$this->db->select('id')
  ->from('users')

  ->where('(first_name', $first_name)
  ->where('age', $this->db->escape($age1).')', FALSE)

  ->or_where('(last_name', $last_name)
  ->where('age', $this->db->escape($age2).')', FALSE)

  ->get();

Let me know if you have a better way to do this.

No Comments |