CodeIgniter 4 Query Builder set() function with update()

Joshua Otwell
Level Up Coding
Published in
5 min readDec 22, 2021

--

CodeIgniter 4 Query Builder class has a update() function used to process UPDATE Data Manipulation Language (DML) commands. Using update() as a standalone function call is perfectly valid. However, there is also a set() function used for setting column values as you would with the SET keyword in an SQL UPDATE statement. Used in conjunction with the Query Builder where() function, you can easily UPDATE column values for an individual row or multiple rows. Continue reading for more information…

Image by OpenIcons from Pixabay

Disclosure: Some of the services and products links in this post are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!

OpenLamp.tech, the Newsletter for PHP and MySQL Developers. Subscribe and join today. Learn and grow. Help build a community.

I’m using a simple ‘employees‘ table (MySQL) having this fictitious data for the examples:

I have this basic EmployeeModel with an all_emps() method which provides all the data from the ’employees’ table as shown in the screenshot above:

Query Builder set(), update, and where() functions

I’ve created this updateEmpLastName() method in the Model and used the Query Builder set(), update() and where() functions to edit an employee’s last name:

Then we can call the model updateEmpLastName() method in a Controller method I…

--

--

SQL | PHP | Photography. Sign-up for my free developer newsletter, OpenLampTech, here: openlamptech.substack.com