Welcome Guest, Not a member yet? Register   Sign In
Help Active Records Query
#1
Question 

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.
Reply
#2

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');
Reply
#3

(This post was last modified: 01-21-2015, 09:14 AM by Hobbes.)

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.
"I reject your reality and substitute my own" - Adam Savage, M5 Inc.
Reply
#4

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)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB