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

PHP AJAX Live Search Tutorial

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

πŸ“Œ Key Takeaways

  • PHP AJAX Live Search Tutorial
  • What Is Live Search in PHP AJAX?
  • Why Use AJAX Live Search?
  • Technologies Used in PHP AJAX Live Search
  • Prerequisites for Live Search Tutorial
  • Sample MySQL Table Used for Live Search
Advertisement

PHP AJAX Live Search – Complete Beginner Tutorial

A live search allows users to see search results while typing, without clicking a submit button or reloading the page. This feature is commonly used in search boxes, product filters, user lists, and admin panels.

This tutorial explains how to build a PHP AJAX live search using MySQL, with clear examples and outputs.


What Is Live Search in PHP AJAX?

Live search is a technique where:

  • JavaScript listens to user input
  • AJAX sends the typed text to PHP
  • PHP searches the database using MySQL
  • Results are returned instantly
  • The page updates without reload

Live search improves:

  • User experience
  • Search speed
  • Website interactivity
  • Data filtering efficiency

Examples include:

  • User search
  • Product search
  • Blog article search
  • Email or username lookup

  • HTML for input field
  • JavaScript for AJAX request
  • PHP for server-side processing
  • MySQL for database search

Prerequisites for Live Search Tutorial

Before starting, ensure you know:

  • Basic HTML
  • Basic JavaScript
  • PHP fundamentals
  • MySQL SELECT and LIKE

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

PHP MySQL Database Connection File

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

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

Step 1: Create HTML Search Input

<!DOCTYPE html>
<html>
<head>
    <title>PHP AJAX Live Search</title>
    <script>
        function liveSearch(value) {
            if (value.length == 0) {
                document.getElementById("result").innerHTML = "";
                return;
            }

            var xhr = new XMLHttpRequest();
            xhr.open("GET", "search.php?query=" + value, true);

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            };

            xhr.send();
        }
    </script>
</head>
<body>

<input type="text" onkeyup="liveSearch(this.value)" placeholder="Search user...">
<div id="result"></div>

</body>
</html>

Step 2: Create PHP Search Script (search.php)

<?php
include "db.php";

$query = $_GET['query'];

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

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['name'] . " (" . $row['email'] . ")<br>";
    }
} else {
    echo "No results found";
}
?>

Output While Typing “ra”

Rahul (rahul@gmail.com)
Ravi (ravi@gmail.com)

Results update instantly without page refresh.


Live Search with Prepared Statement (Secure Version)

<?php
include "db.php";

$stmt = mysqli_prepare($conn,
    "SELECT * FROM users WHERE name LIKE ?");
$search = "%" . $_GET['query'] . "%";
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>";
}
?>

Improve Live Search with LIMIT

SELECT * FROM users WHERE name LIKE '%keyword%' LIMIT 5;

This improves performance for large datasets.


  • Not validating input
  • Missing debounce for fast typing
  • Searching without LIMIT
  • Not handling empty input

PHP AJAX live search, live search PHP MySQL, AJAX real time search PHP, PHP AJAX search example, PHP MySQL live search tutorial


  • Use prepared statements
  • Limit search results
  • Add indexes on search columns
  • Use debounce for better performance
  • Sanitize user input


Frequently Asked Questions (FAQ)

1. What is live search in PHP?

It displays search results while the user types.

2. Does live search require page reload?

No, it works without reloading.

3. Is live search secure?

Yes, when using prepared statements.

4. Can live search work with large databases?

Yes, with LIMIT and indexing.

Yes, for real-time updates.

P
php Guru
← Previous Post
PHP Example – AJAX and XML Tutorial
Next Post β†’
PHP AJAX Autocomplete Tutorial

Leave a Reply

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

Prove your humanity: 5   +   7   =