CodeIgniter Forums
Help Active Records Query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Help Active Records Query (/showthread.php?tid=816)



Help Active Records Query - CI_Fellow - 01-20-2015

Here My SQL Query

Code:
SELECT `id`, `name`, `dob` FROM (`tbl_personal`) WHERE `group_id` =  '666' AND (`fname` = 'Devid' OR `fname` = 'Jhone'')

I want to know how to write this query using CI Active records ?

Thanks.


RE: Help Active Records Query - rajneeshgobin - 01-20-2015

try something like this

$this->db->select('id');
$this->db->select('name');
$this->db->select('dob');
$this->db->where('group_id',666);
$this->db->where('fname','devid');
$this->db->or_where('fname','jhone');
$this->db->from('tbl_personal');


RE: Help Active Records Query - Hobbes - 01-20-2015

try something like this in your model class:

Code:
public function get_personal() {
$this->db->select('id, name, dob');
$this->db->from('personal'); //assuming 'tbl_' is your table prefix defined in application/config/database.php
$this->db->where(array('group_id' => 666, 'fname' => 'Devid'));
$this->db->or_where('fname', 'Jhone');
$query = $this->db->get();

return $query->result(); // return an object

//if you want to return an array comment out the above line and uncomment the below line:
//return $query->result_array();
}

also just to point out, you may want to read the documentation.


RE: Help Active Records Query - CroNiX - 01-21-2015

The answer really depends on which version of CI you're using. CI3 has some advanced features to build stuff like that... AND (something OR something_else)