Reneesh Kurian
Reneesh Kurian

Reputation: 105

Codeigniter method for update

$age=10;
$id=1;
$sql='update student set age=age+$age where sid=$id;

I want to write this sql statement using Codeigniter how is that possible?Any native way in Codeigniter to do this? I am using Codeigniter active record

Upvotes: 2

Views: 315

Answers (4)

Mohit Shukla
Mohit Shukla

Reputation: 65

you can use

$data = array(
  "age"=>"age+$age"
);
$this->db->set($data);
$this->db->where('id',$id);
$this->db->update('student');

for more info follow https://www.codeigniter.com/userguide3/database/query_builder.html

Upvotes: 0

Shihas
Shihas

Reputation: 814

You can simply do like this:

$age = 10;
$id = 1;
$data = array('age' => "age+$age");

$this->db->where('sid',$id);
$this->db->update('student',$data);  

Using $this->db->set($data); can be avoided.

Upvotes: 0

Ankit Doshi
Ankit Doshi

Reputation: 1174

Let me edit my answer

$data = array("age"=>"age+$age");
$this->db->set($data);
$this->db->where('sid',$id);
$this->db->update("student",$data);

Upvotes: 1

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Try like this....

$this->db->set('age', "age+$age",FALSE);
$this->db->where('sid', $id);
$this->db->update('student');

OR

$data = array("age"=>"age+$age");
$this->db->set($data);
$this->db->where('sid',$id);
$this->db->update('student');

For more see deatais here https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data

Upvotes: 4

Related Questions