CodeIgniter 4 Query Parameter Binding with examples in MySQL

As powerful and useful as the CodeIgniter 4 Query Builder class methods and functions are, there are times when you need to hand-craft your own queries. Perhaps they are complex. Or, you would just rather write the raw SQL. Learn how to safely use the input you need – typically as part of the WHERE clause conditional(s) – using 2 different parameter binding variations with examples in MySQL.

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.

Query Builder where() function

Related: Read the detailed guide I wrote on the CodeIgniter 4 Query Builder where() function for more information about the various parameter structures available.

For the examples in this blog post, I’ll be logging and showing the queries that are sent to the server by using the $db->getLastQuery() method. Read and learn more about $db->getLastQuery() in this article.

Positional Parameter Bindings

Positional parameter bindings (?) are defined in the SQL standard and are supported for CodeIgniter 4 as well.

In this first example, I use the WHERE clause conditional filter, `name` = ?, and provide the filter value ‘Drama’ in the $db->query() methods’ 2nd array() parameter:

And below we see the query sent to the server as logged from $db->getLastQuery():


Help support this blog and content with a tip. Thank you!


Named Bindings

Not only can you use the positional parameter bindings but, named placeholder bindings are allowed in the queries as well.

Simply surround the named bindings with colons and use an associative array as the 2nd parameter in the $db->query() method:

Again, we see the exact query sent to the server as logged from $db->getLastQuery():


πŸ“° Get your brand, product, or service the attention it needs with affordable classified ad placement in the OpenLampTech newsletter. Thank you for your support!


Positional Parameter Bindings for IN()

If you need to use the IN() operator in any queries, positional parameter bindings can be used for this as well.

Place the ‘?‘ after the IN keyword (without any parentheses – they will be included automatically), and include an array of values inside the array 2nd parameter for $db->query():

Notice the query has the IN() operator with values.


Although the example queries in this article are quite simple, you now have an idea of how to use parameter binding in CodeIgniter 4 with the query() method in those instances you have more complex querying needs.


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.

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.


πŸ“° Get your brand, product, or service the attention it needs with affordable classified ad placement in the OpenLampTech newsletter. Thank you for your support!


Hey thanks for commenting! Leave a Reply

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