Organizational Research By

Surprising Reserch Topic

combining mysql and or queries in codeigniter

combining mysql and or queries in codeigniter  using -'php,mysql,codeigniter,codeigniter-2'

I want to combine AND OR mysql queries in CI. I have already seen this thread:
But they don't provide the exact solution there.

How do I create the following query using strictly the CI framework? (I can create the query easily without the brackets but then it is not the same query.)

LastName='Svendson' AND Age="12" AND
(FirstName='Tove' OR FirstName='Ola' OR Gender="M" OR Country="India")

P.S.: This is just a sample query even if it makes no sense & Do not suggest writing the entire OR part of the query inside a single where().

EDIT: Basically I want the implementation of the following simple query:

SELECT * FROM `table` WHERE field1='value1' AND (field2='value2' OR field3='value3')


asked Sep 15, 2015 by LenoreHatele
0 votes

Related Hot Questions

3 Answers

0 votes

and this will work?

$this->db->where('LastName', 'Svendson');
$this->db->where('Age', 12);
$this->db->where("(FirstName='Tove' OR FirstName='Ola' OR Gender='M' OR Country='India')", NULL, FALSE);
$query = $this->db->get('Persons');
return $query->result();
answered Sep 15, 2015 by TereseBaldwi
0 votes

In CodeIgniter 3 there are new methods group_start() and group_end() which serve exactly for this purpose.

$query = $this->db
     ->where('LastName', 'Svendson');
     ->where('Age', 12);
return $query->result();
answered Sep 15, 2015 by ValenciaMcCa
0 votes

Currently with CI2 you can't access the Query Builder method ($this->db->_compile_select() ) of the Database class without extending the Database class and changing the method's access type from private to public/protected, that kills the ability to build Subquery's like your trying to build using the ActiveRecord class. The only method to make a subquery like the one your trying to build would be to just use the db query method

$table = $this->db->dbprefix('tablename');

$sql = "SELECT * FROM `{$table}` WHERE field1='?' AND (field2='?' OR field3='?') ";

There was a blog post about doing this at CI Subquerys but it's out of date and only works on CI 1.7 Hope that helps a bit.

answered Sep 15, 2015 by TanishaNKLJw