Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple where condition codeigniter

Tags:

codeigniter

How can I convert this query to active record?

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

I tried to convert the query above to:

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

How about using the where clausa status?

# must i write db->where two times like this? $this->db->where('username',$username); $this->db->where('status',$status); 

I also tried this:

$this->db->where('username',$username,'status',$status); 
like image 907
Zulkifli Said Avatar asked May 10 '12 16:05

Zulkifli Said


People also ask

Does CodeIgniter prevent SQL injection?

SQL injection is an attack made on database query. In PHP, we are use mysql_real_escape_string() function to prevent this along with other techniques but CodeIgniter provides inbuilt functions and libraries to prevent this.

Where is the code igniter?

CodeIgniter Select Query with $this->db->where_in()The where_in() function is used to generate WHERE field IN ('item', 'item') SQL query string joined with AND if appropriate.


2 Answers

you can use an array and pass the array.

Associative array method: $array = array('name' => $name, 'title' => $title, 'status' => $status);  $this->db->where($array);   // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' 

Or if you want to do something other than = comparison

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);  $this->db->where($array); 
like image 112
NDBoost Avatar answered Sep 22 '22 21:09

NDBoost


Yes, multiple calls to where() is a perfectly valid way to achieve this.

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

http://www.codeigniter.com/user_guide/database/query_builder.html

like image 30
IsisCode Avatar answered Sep 21 '22 21:09

IsisCode