Search data from SQL database and display results in same page PHP code

 We can Search data from SQL database and display results in same page.

Search data from SQL database and display results in same page


Here are some example of  SQL search result display:


let, 

database : admin

username : kali

password : kali12345

tabledirectory : datax

column: firstname

column: lastname


Try your stuff with following code :



<?php 

$username = "kali"; 

$password = "kali12345"; 

$database = "admin"; 

$mysqli = new mysqli("localhost", $username, $password, $database); 

$query = "SELECT * FROM datax";

?>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Search</title>

</head>


<body>

    <form action="" method="POST">

        <input type="text" placeholder="Search" name="search">

        <button type="submit" name="submit">Search</button>

    </form>

<?php



if (isset($_POST['submit'])) {

    $searchValue = $_POST['search'];

    $con = new mysqli("localhost", "kali", "kali12345", "admin");

    if ($con->connect_error) {

        echo "connection Failed: " . $con->connect_error;

    } else {

        $sql = "SELECT * FROM datax WHERE firstname LIKE '%$searchValue%'";


        $result = $con->query($sql);

        while ($row = $result->fetch_assoc()) {

            echo $row['firstname'] . "<br>";

            echo $row['lastname'] . "<br>";

        }


      

    }   

}




?>

</body>


</html>

Previous Post Next Post