MySQL Like Operator in CodeIgniter 4 Query Builder Tutorial

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 –

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')

Example #1

Task

We want to list all students whose name starts from S character.

MySQL Query

SELECT * FROM students WHERE name LIKE 'S%'

Writing in CodeIgniter 4

We will use query builder methods for this.

Read More[…]

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors