Welcome Guest, Not a member yet? Register   Sign In
multiple where condition codeigniter
#1

[eluser]Zulkifli Said[/eluser]
how to convert this query to active record??

Code:
"UPDATE table_user SET email = '$email', last_ip = '$last_ip' where username = '$username' and status = '$status'";
???

i try to convert the query above to :

Code:
$data = array('email' => $email, 'last_ip' => $ip);
$this->db->where('username',$username);
$this->db->update('table_user',$data);

my question is?? how about with where clausa status???

must i write db->where two times like this?

Code:
$this->db->where('username',$username);
$this->db->where('status',$status);

or

one time like this

Code:
$this->db->where('username',$username,'status',$status);
#2

[eluser]Brad K Morse[/eluser]
Yes, you will need to use two separate where()

Code:
$data = array('email' => $email, 'last_ip' => $ip);

//method chaining
$this->db
->where('username', $username)
->where('status', $status)
->update('table_user', $data);

// or this way
$this->db->where('username', $username);
$this->db->where('status', $status);
$this->db->update('table_user', $data);
#3

[eluser]CroNiX[/eluser]
Or pass it an array (they will all be connected by ANDs)
Code:
$where = array(
  'username' => $username,
  'status'   => $status
);
$this->db->where($where);
#4

[eluser]Samus[/eluser]
[quote author="CroNiX" date="1336668947"]Or pass it an array (they will all be connected by ANDs)
Code:
$where = array(
  'username' => $username,
  'status'   => $status
);
$this->db->where($where);
[/quote]
this
#5

[eluser]ninjayan[/eluser]
I'm also having a problem using multiple where / or_where.
Here's the scenario:
I am checking on the database if there's a record with a 'name' or 'code' which the 'id' is not equal to the given.

Before I do the checking, I fetch a row id,name,code for example (1,office of the city mayor, ocm) then I can update either the name or code and the checking enters. If the name or code exist where id is not equal to the fetched id, then it should return false

code
Code:
$where = array(
   'department_id !=' => $this->input->post('id'),
   'name' => $this->input->post('department')
  );
  $check = $this
     ->db
     ->where($where)
     ->or_where('dep_code', $this->input->post('code'))
     ->get('departments');

  if ($check->num_rows() > 0)
  {
   return false;
  }
  else
  {
   return true;
  }

If I put a code which does not exist it works fine, but if I change/put a name that does not exist and not changing the code. it returns false.
#6

[eluser]Brad K Morse[/eluser]
Try below. I have never stored a != in an array, so maybe that is throwing off the where.

Code:
$check = $this
     ->db
     ->where('department_id !=', $this->input->post('id') )
     ->where('name', $this->input->post('department') )
     ->or_where('dep_code', $this->input->post('code'))
     ->get('departments');

  if ($check->num_rows() > 0)
  {
    return false;
  } else {
   return true;
  }
#7

[eluser]ninjayan[/eluser]
thank you for replying. I also did that before but still no good. I think the grouping of statements is the problem

Code:
where department_id != $id AND (name = $name OR code = $code)

^what I'm trying to do.
#8

[eluser]Aken[/eluser]
http://ellislab.com/forums/viewthread/233167/




Theme © iAndrew 2016 - Forum software by © MyBB