πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

PHP MySQL Search Using LIKE – Build Powerful Database Search

P
php Guru
Β· January 8, 2026 Β· 4 min read Β· Updated January 8, 2026

πŸ“Œ Key Takeaways

  • PHP MySQL Search Using LIKE – Build Powerful Database Search
  • What Is LIKE Operator in MySQL?
  • Why Use LIKE for Searching Data?
  • Understanding Wildcards in LIKE Operator
  • Prerequisites for PHP MySQL Search
  • Sample MySQL Table Used for Search
Advertisement

PHP MySQL Search Using LIKE – Complete Beginner Tutorial

Search functionality is one of the most important features of dynamic websites. Whether it’s a user search, product search, or blog search, the MySQL LIKE operator allows flexible and partial matching of data.

This tutorial explains how to search data in MySQL using PHP and LIKE, with real examples and output.


What Is LIKE Operator in MySQL?

The LIKE operator is used in SQL to search for a specified pattern in a column.

Basic syntax:

SELECT * FROM table_name WHERE column LIKE 'pattern';

Why Use LIKE for Searching Data?

The LIKE operator helps to:

  • Perform partial searches
  • Match keywords anywhere in text
  • Build search boxes
  • Improve user experience

Understanding Wildcards in LIKE Operator

Two wildcards are commonly used:

  • % β†’ Matches any number of characters
  • _ β†’ Matches exactly one character

Example:

LIKE '%php%'

This matches any value containing β€œphp”.


Before implementing search, ensure you have:

  • PHP installed
  • MySQL database
  • A working database connection
  • A table with searchable data

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

PHP MySQL Database Connection Code

<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");

if (!$conn) {
    die("Database connection failed");
}
?>

Basic Search Using LIKE in PHP MySQL

<?php
$search = "Rahul";

$query = "SELECT * FROM users WHERE name LIKE '%$search%'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . "<br>";
}
?>

Output:

Rahul

Dynamic Search Using HTML Form and LIKE

HTML Search Form

<form method="get">
    <input type="text" name="keyword" placeholder="Search name">
    <input type="submit" value="Search">
</form>

PHP Search Script

<?php
if (isset($_GET['keyword'])) {
    $keyword = $_GET['keyword'];

    $query = "SELECT * FROM users WHERE name LIKE '%$keyword%'";
    $result = mysqli_query($conn, $query);

    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['name'] . "<br>";
    }
}
?>

Output (Keyword: ra):

Rahul
Ravi

Search Multiple Columns Using LIKE

<?php
$keyword = "gmail";

$query = "SELECT * FROM users
          WHERE name LIKE '%$keyword%'
          OR email LIKE '%$keyword%'";

$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['email'] . "<br>";
}
?>

Output:

rahul@gmail.com
amit@gmail.com

Search Data Starting or Ending With Keyword

LIKE 'A%'   -- starts with A
LIKE '%a'   -- ends with a

Example in PHP

<?php
$query = "SELECT * FROM users WHERE name LIKE 'A%'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . "<br>";
}
?>

Output:

Amit
Anil

Secure Search Using Prepared Statements

Prepared statements protect against SQL injection.

<?php
$stmt = mysqli_prepare($conn,
    "SELECT * FROM users WHERE name LIKE ?");
$search = "%ra%";
mysqli_stmt_bind_param($stmt, "s", $search);

mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . "<br>";
}
?>

Output:

Rahul
Ravi

Search with Pagination Using LIKE

LIKE is often combined with pagination.

SELECT * FROM users
WHERE name LIKE '%php%'
LIMIT $offset, $records_per_page;

Common Mistakes When Using LIKE in PHP MySQL

  • Not escaping user input
  • Forgetting wildcards
  • Using LIKE on non-indexed columns
  • Searching large tables without LIMIT

PHP MySQL search using LIKE, PHP search MySQL LIKE, PHP MySQL search tutorial, PHP MySQL wildcard search, PHP database search example, PHP MySQL tutorial


  • Use prepared statements
  • Use LIMIT for large datasets
  • Add indexes for searchable columns
  • Combine search with pagination
  • Sanitize and validate input


Frequently Asked Questions (FAQ)

1. What does LIKE do in MySQL?

It searches for patterns in column values.

2. What is % in LIKE?

It matches any number of characters.

3. Is LIKE case-sensitive?

Depends on the column collation.

4. Is LIKE slow for large data?

Yes, without indexes and LIMIT.

5. Can LIKE be used with numbers?

Yes, but mainly used with text.

P
php Guru
← Previous Post
PHP MySQL Pagination Full Tutorial – Create Powerful & Fast Pagination
Next Post β†’
PHP Example – AJAX and XML Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 10   +   8   =