CodeIgniter 4 Query Builder class has an 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…
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.
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!
The Newsletter for PHP and MySQL Developers
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 use 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 have created named, updateLastName():
You can see in the screenshot below, after calling the Controller updateLastName() method, employee ‘Jessica Rabbit’ now has the last name of ‘Jones’:
If you find value in the content on my blog and want to support my work, you can toss some spare change in my Tip Jar. Thank you so much!!!
Query Builder set() function – multiple calls through chaining
Multiple calls to the Query Builder set() function are possible by chaining them one after the other, allowing you to set values on multiple columns at one time. In this next Model method, updateEmpFirstAndLastName(), I chain 2 Query Builder set() functions together; one for the ‘first_name’ column value and one for the ‘last_name’ column:
Again, focussing on fictional employee 3, ‘Jessica Jones’, I update both ‘first_name’ and ‘last_name’ columns with new data. Results are shown in the follow-up screenshot:
Are you a Medium member? If so, receive an email notification each time I publish a blog post if you prefer the Medium platform. Not a member? No worries! Use my sign-up link (I will get a commission at no extra cost to you) and join. I really enjoy reading all the great content there and I know you will too!!!
CodeIgniter 4 Query Builder informational resources
Visit the official CodeIgniter 4 documentation on any of the functions covered in the post:
As always, if you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate blog posts and are much appreciated. Thank you for reading.
The Query Builder set() function can be used with insert() function calls as well and I plan to cover that use in a future blog post. Be sure to subscribe to my free newsletter, OpenLamp.tech, the newsletter for PHP/MySQL developers, and stay up to date on PHP and SQL-related content.
Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.
The Newsletter for PHP and MySQL Developers
How can I help you?
- Are you thinking of starting up a blog? I use WordPress for my blog. Let’s both save money on the plans offered. 💸
- Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. ✉️
- Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.
- I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.
- Grab a free pack of mobile Creator wallpapers.