Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update the value of a field in database by 1 using codeigniter

I want to implement a SQL statement using codeigniter active record.

UPDATE tags SET usage = usage+1 WHERE tag="java";

How can I implement this using Codeigniter active records?

Regards

like image 320
vikmalhotra Avatar asked Jul 22 '10 00:07

vikmalhotra


People also ask

What does this -> db -> update return?

You can use $this->db->affected_rows() in Codeigniter this returns a numeric value when doing "write" type queries (insert, update, etc.). In MySQL DELETE FROM TABLE returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows.

How do you check data is updated or not in CodeIgniter?

Mirov When we are working with codeigniter, the data only updated when there is some change in the input field's value and then the $this->db->affected_rows() will return value greater then 0.

What is Query Builder in CodeIgniter?

CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

How to update existing records in database table with CodeIgniter?

Update records in Database Table with CodeIgniter. The Codeigniter allows us to perform the database action like – select, insert, update, and delete with the minimal script. The $this->db->update() method is used for update existing record.

How to use update () method in CodeIgniter?

In CodeIgniter, update () method is used to update existing record in database table. In order to generate update statement, update () method is used along with set () and where () methods in following ways – $set (array):- An associative array of column/value for update.

How to perform database action using CodeIgniter?

The Codeigniter allows us to perform the database action like – select, insert, update, and delete with the minimal script. The $this->db->update () method is used for update existing record. It takes table name and an array or object as the parameter. In the demonstration, I am listing the user’s list with the edit option.

Can I use CodeIgniter to update a MySQL count field +1?

I always use the CodeIgniter models to do all the MySQL querys (insert, select, updates...) but this won't work if you want to update a count field +1.


2 Answers

$this->db->set('usage', 'usage+1', FALSE);
$this->db->where('tag', 'java');
$this->db->update('tags');
like image 196
Phil Sturgeon Avatar answered Sep 25 '22 12:09

Phil Sturgeon


You can also use something like this

$data = array('usage' => 'usage+1', *other columns*);
$this->db->where('tag', 'java');
$this->db->update('tags', $data);

UPDATE: $data was not being passed on to update

like image 38
Srihari Goud Avatar answered Sep 24 '22 12:09

Srihari Goud