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

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…

Image by OpenIcons from Pixabay 

Disclosure: Some of the services and product 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!


The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

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

All employee information.

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’:

Updated the last_name column using Query Builder set(), where(), and update() functions.

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:

Updated the first_name and last_name columns using Query Builder set(), update(), and where() functions.

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.

Closing

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.



To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

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

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

How can I help you?

Disclosure: Some of the services and product 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.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.