CodeIgniter 4 provides the complete set of Query builder methods to use in querying database. Inside this article we will see the concept of MySQL Like Operator in codeigniter 4 Query builder.
In Query builder to use Like operator we have a method available. Like operator helps us to get data with a certain pattern with a different different string format.
MySQL Like Operator uses % or _ in query to create pattern. % represents multiple characters, _ is for single character.
In CodeIgniter, It uses only % to create search patterns.
Learn More –
- DataTable Excel Data Export in CodeIgniter 4 Tutorial
- Encryption Service in CodeIgniter 4 Tutorial
- Export Data Into Excel Report In CodeIgniter 4 Tutorial
- ext-intl PHP Extension Error CodeIgniter 4 Installation
Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.
Table of Contents
Create Database & Table in Application
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE codeigniter4_app;
Next, we need a table. That table will be responsible to store data. Let’s create a table with some columns.
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`phone_no` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Successfully, we have created a database and a table.
CodeIgniter 4 Like Operator Method
In CodeIgniter 4, like() named method available to do like pattern queries.
$builder->like('column_name','match', 'before | after | both')
We want to list all students whose name starts from S character.
SELECT * FROM students WHERE name LIKE 'S%'
Writing in CodeIgniter 4
We will use query builder methods for this.