how to fetch data from database in php using search button?

Today, We want to share with you php code search database and display results in table.In this post we will show you How To Find Data In MySQL Database Table By Id And Show The Results In Form Inputs In Php using MySQLI, hear for Php Code To Search Data In Mysql Database Table And Dispaly Results In Inputs Using MySQLI. we will give you demo and example for implement.In this post, we will learn about How To Search And Display Data From Database In Php with an example.

php mysql search database and display results

<?php
    $con= new mysqli("localhost","root","[email protected]","Members");
    $name = $_post['search'];
    //$query = "SELECT * FROM members
   // WHERE fname LIKE '%{$name}%' OR lname LIKE '%{$name}%'";

    // Check here database connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

$result = mysqli_query($con, "SELECT * FROM members
    WHERE fname LIKE '%{$name}%' OR lname LIKE '%{$name}%'");

		while ($row = mysqli_fetch_array($result))
		{
		        echo $row['fname'] . " " . $row['lname'];
		        echo "<br>";
		}
    mysqli_close($con);
    ?>

How to Display MySQL Table Data Tutorial

index.php

<html>
<body>
<?php 
$username = "root"; 
$password = "Pajkd875454"; 
$database = "pakainfo_shop"; 
$mysqli = new mysqli("localhost", $username, $password, $database); 
$query = "SELECT * FROM products";


echo '<table border="0" cellspacing="2" cellpadding="2"> 
      <tr> 
          <td> <font face="Arial">Product Value1</font> </td> 
          <td> <font face="Arial">Product Value2</font> </td> 
          <td> <font face="Arial">Product Value3</font> </td> 
          <td> <font face="Arial">Product Value4</font> </td> 
          <td> <font face="Arial">Product Value5</font> </td> 
      </tr>';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $inputdata1name = $row["col1"];
        $inputdata2name = $row["col2"];
        $inputdata3name = $row["col3"];
        $inputdata4name = $row["col4"];
        $inputdata5name = $row["col5"]; 

        echo '<tr> 
                  <td>'.$inputdata1name.'</td> 
                  <td>'.$inputdata2name.'</td> 
                  <td>'.$inputdata3name.'</td> 
                  <td>'.$inputdata4name.'</td> 
                  <td>'.$inputdata5name.'</td> 
              </tr>';
    }
    $result->free();
} 
?>
</body>
</html>

Free Live Chat for Any Issue

HTML code

<form action="insert.php" method="post">
    Product Value1: <input type="text" name = "productval1" /><br/>
    Product Value2: <input type="text" name = "productval2" /><br/>
    Product Value3: <input type="text" name = "productval3" /><br/>
    Product Value4: <input type="text" name = "productval4" /><br/>
    Product Value5: <input type="text" name = "productval5" /><br/>
    <input type="submit" />
</form>

insert.php

<?php
$username = "root";
$password = "JVirat987001";
$database = "pakainfo_base";

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

$inputdata1 = $mysqli->real_escape_string($_POST['productval1']);
$inputdata2 = $mysqli->real_escape_string($_POST['productval2']);
$inputdata3 = $mysqli->real_escape_string($_POST['productval3']);
$inputdata4 = $mysqli->real_escape_string($_POST['productval4']);
$inputdata5 = $mysqli->real_escape_string($_POST['productval5']);

$query = "INSERT INTO products (col1, col2, col3, col4, col5)
            VALUES ('{$inputdata1}','{$inputdata2}','{$inputdata3}','{$inputdata4}','{$inputdata5}')";

$mysqli->query($query);
$mysqli->close();

Download

I hope you get an idea about how to search and filter data in html table using php and mysql database [ with source code ].
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.