Php : How To Search And Filter Data In Html Table Using Php And MySQL

Php How To Filter Data In Html Table Using Php And MySQL


________________________________________________________


In this Php Tutorial we will see How To Use Search Data In Html Table Using MySQL Database And Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
    
}
 else {
    $query = "SELECT * FROM `users`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "test_db");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="php_html_table_data_filter.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
            
            <table>
                <tr>
                    <th>Id</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Age</th>
                </tr>

                <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['id'];?></td>
                    <td><?php echo $row['fname'];?></td>
                    <td><?php echo $row['lname'];?></td>
                    <td><?php echo $row['age'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>



///////////////OUTPUT: 

php html table find
Search For W




                     




19 commentaires:

  1. Thanks. The script work in my local server but couldnt fetch database record when i uploaded the files to online server

    RépondreSupprimer
  2. i find that error at line 5
    Parse error: syntax error, unexpected ' ' (T_STRING) in C:\wamp64\www\phpsql\afffiche.php on line 5

    RépondreSupprimer
  3. Error object not found how to fix it

    RépondreSupprimer
  4. Error object not found how to fix it

    RépondreSupprimer
  5. Nice code. It works fine. Although I had to make some changes.

    RépondreSupprimer
  6. Thank you. Your code solved my problem/

    RépondreSupprimer
  7. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\bluesky\search.php on line 69

    RépondreSupprimer
  8. Thank you I applied the code .. I have the problem (
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\bluesky\search.php on line 69

    RépondreSupprimer
  9. Can I hide the data and show only the search result? Also how can I use exact search instead of filter?

    RépondreSupprimer
  10. you saved my minor project!!!!!!!!!!
    thank you so much!!

    RépondreSupprimer
  11. CONCAT allow you to join two or multiple character strings together

    RépondreSupprimer
  12. Hi. Thanks for sharing this. Is it possible to use a similar approach when searching using more than one parameter?

    RépondreSupprimer
  13. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\CMP\search.php on line 53


    I Have this problem it is the do while statement in the html

    RépondreSupprimer
  14. I have problem with this statement
    Client & Server-side logic (PDO/XML processing & JSON) to display suitable HTML tables

    RépondreSupprimer
  15. how to connect this 2 code together
    data set (database table or XML file, including field headings), once a table or file has been selected;

    RépondreSupprimer
  16. NOT WORKING! IT CAN'T SEARCH OR FILTER!
    I PASTE EVERYTHING TO MAKE SURE BUT IT STILL NOT WORK

    RépondreSupprimer