PHP Ajax Search MySQL Database Example

Mar 24, 2022 . Admin

Hello dev,

I am going to explain you how to PHP ajax live search mysql database. You will learn Ajax search dropdown list PHP from database MySQL phpmyadmin. In side this article we will see Ajax search dropdown list PHP from database MySQL.

This article will give you simple example of Ajax live data search using jquery PHP MySQL. We will use get simple how to create search bar in Ajax with PHP and MySQL.

I will give you simple Example of How to PHP Ajax MySQL live search example.

So, let's see bellow solution:

    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $dbname = "demos";
    $connection = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
        die('Database connection error : ' .mysql_error());
<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Ajax Search MySQL Database -</title>
    <link href="" rel="stylesheet">
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12 h-75">
                <div class="card w-75 m-auto" style="height: 300px;">
                    <div class="card-header alert alert-info text-center mb-3">
                        <h2>PHP Ajax Search MySQL Database -</h2>
                    <input type="text" class="form-control w-50" name="live_search" id="live_search" autocomplete="off" placeholder="Search ..." style="margin-left: 20px;">
                    <div id="search_result" style="margin-left: 20px;"></div>
    <script src=""></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#live_search").keyup(function () {
                var query = $(this).val();
                if (query != "") {
                        url: 'ajax-live-search.php',
                        method: 'POST',
                        data: {
                            query: query
                        success: function (data) {
                            $('#search_result').css('display', 'block');
                            $("#live_search").focusout(function () {
                                $('#search_result').css('display', 'none');
                            $("#live_search").focusin(function () {
                                $('#search_result').css('display', 'block');
                } else {
                    $('#search_result').css('display', 'none');
    require_once "db.php";
    if (isset($_POST['query'])) {
        $query = "SELECT * FROM categor WHERE name LIKE '{$_POST['query']}%' LIMIT 100";
        $result = mysqli_query($connection, $query);

        if (mysqli_num_rows($result) > 0) {
            while ($res = mysqli_fetch_array($result)) {
                echo $res['name']. "<br/>";

        } else {
            echo "<div class='alert alert-danger mt-3 text-center' role='alert'>Song not found</div>";

It will help you...