PHP - Mysql LIKE Operator
Last Updated :
07 Apr, 2021
Problem Statement :
In this article, we are going to display data using LIKE operator with SQL in Xampp server.
Here we are going to consider the student address database as an example.
Requirements:
Xampp
Introduction:
PHP stands for hypertext preprocessor. It is used as a server-side scripting language and can be used to connect with MySQL server with xampp tool.
MySQL is a query language for managing databases.
The LIKE operator in SQL is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards that can be used in conjunction with the LIKE operator. They are:
- The percent sign (%) which represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character.
Syntax:
SELECT column1, column2, ...,columnn
FROM table_name
WHERE columnn LIKE pattern;
Description
- letter% = gives the result starts with the given letter
Example:
Consider the following table:
Query:
Address starts with h:
SELECT * from student_address WHERE saddress LIKE 'h%'
Output:
Address starts with h:
STUDENT-ID : 3 ----- NAME : ojaswi ----- ADDRESS : hyderabad
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : 5 ----- NAME : gnanesh ----- ADDRESS : hyderabad
Query:
Name ends with h:
SELECT * from student_address WHERE sname LIKE '%h';
Output:
Name ends with h:
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : 5 ----- NAME : gnanesh ----- ADDRESS : hyderabad
Query:
Address contains "um" pattern
SELECT * from student_address WHERE sname LIKE '%um%';
Output:
STUDENT-ID : 1 ----- NAME : sravan kumar ----- ADDRESS : kakumanu
Query:
Address starts with r and ends with h.
SELECT * from student_address WHERE sname LIKE 'r%h';
Output:
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad.
Approach:
- Create database(named database) and create table named student_address
- Insert data into the table using PHP
- Write PHP code to perform like operation
- Observe the results
Steps:

- Create database named database and create a table named student_address

- Write PHP code to insert records into it. (data1.php)
PHP
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
// Check this connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql = "INSERT INTO student_address VALUES (1,'sravan kumar','kakumanu');";
$sql .= "INSERT INTO student_address VALUES (2,'bobby','kakumanu');";
$sql .= "INSERT INTO student_address VALUES (3,'ojaswi','hyderabad');";
$sql .= "INSERT INTO student_address VALUES (4,'rohith','hyderabad');";
$sql .= "INSERT INTO student_address VALUES (5,'gnanesh','hyderabad');";
if ($conn->multi_query($sql) === TRUE) {
echo "data stored successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
open browser and type "localhost.data1.php" to execute it.
Output:
data stored successfully
- PHP code demo for like operator for a letter starts with :
form.php
PHP
<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "Like operator demo: "; echo"</h1>";
echo "<br>";
echo "address starts with h:";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT * from student_address WHERE saddress LIKE 'h%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
echo "<br>";
echo "name starts with s ";
echo "<br>";
echo "<br>";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 's%'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
//close the connection
$conn->close();
?>
</body>
</html>
Output:
localhost/form.php

- PHP code demo for a letter ends with :
form1.php
PHP
<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "Like operator demo: "; echo"</h1>";
echo "<br>";
echo "name ends with h:";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%h'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
echo "<br>";
echo "address ends with u ";
echo "<br>";
echo "<br>";
//sql query
$sql1 = "SELECT * from student_address WHERE saddress LIKE '%u'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
//close the connection
$conn->close();
?>
</body>
</html>
Output:

- PHP code demo for a substring match and letter starts with-ends with
form2.php
PHP
<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "Like operator demo: "; echo"</h1>";
echo "<br>";
echo "address contains um:";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%um%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
echo "<br>";
echo "name starts with r and ends with h ";
echo "<br>";
echo "<br>";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 'r%h'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "<br>";
}
//close the connection
$conn->close();
?>
</body>
</html>
Output:
Similar Reads
PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages, making it essential for building data-driven websites. It allows developers to handle database interactions, session
9 min read
Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo
15+ min read
PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi
8 min read
PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien
5 min read
Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co
7 min read
PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
8 min read
Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET
4 min read
PHP Data Types In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you donât need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable,
4 min read
PHP Operators In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH
8 min read
Creating a Registration and Login System with PHP and MySQL A registration and login system is a fundamental component of many web applications and provides user authentication and security. This allows users to create an account log in with their login credentials and manage their session securely. By using PHP for server-side scripting and MYSQL for databa
12 min read