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 | Bitwise Operators
The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then calculation is performed on the operands. The mathematical operations such as addition , subtraction , multiplication etc. can be performed at bit-level for faster p
5 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
'AND' vs '&&' as operator in PHP
'AND' Operator The AND operator is called logical operator. It returns true if both the operands are true. Example: php <?php // Variable declaration and // initialization $a = 100; $b = 50; // Check two condition using // AND operator if ($a == 100 and $b == 10) echo "True"; else echo
3 min read
PHP 7 | Spaceship Operator
This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by "". This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands. This ope
2 min read
What is the use of Null Coalesce Operator ?
PHP 7 introduced a null-coalescing operator with ?? syntax. This operator returns its first operand if its value has been set and it is not NULL, otherwise it will return its second operand. This operator can be used in a scenario where the programmer wants to get some input from the user and if the
2 min read
PHP | MySQL UPDATE Query
The MySQL UPDATE query is used to update existing records in a table in a MySQL database. It can be used to update one or more field at the same time. It can be used to specify any condition using the WHERE clause. Syntax : The basic syntax of the Update Query is - Implementation of Where Update Que
2 min read
PHP empty() Function
The empty() function in PHP checks whether a variable is empty. It returns true if the variable has a value considered "empty," such as 0, null, false, an empty string, or an unset variable, and false otherwise. Syntaxbool empty ( $var )Parameter: This function accepts a single parameter as shown in
3 min read
Difference between the (=), (==), and (===) operators in PHP
In PHP, the '=' operator is used for assignment, while the '==' operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the '===' operator is used for strict equality comparison, meaning it checks if two value
3 min read
PHP mb_ord() Function
The m_ord() is an inbuilt function in PHP that returns the Unicode code point for the specific character. Syntax: mb_ord(string $string, ?string $encoding = null): int|falseParameters: This function has two parameters: string: This is the string input. It must be a valid string.encoding: The encodin
1 min read
PHP | gmp_or() Function
The gmp_or() is an inbuilt function in PHP which is used to calculate the bitwise OR of two GMP numbers(GNU Multiple Precision : For large numbers). Syntax: gmp_or($num1, $num2) Parameters: This function accepts two GMP numbers, $num1, $num2 as mandatory parameters as shown in the above syntax. Thes
2 min read