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




                     




Share this

Related Posts

Previous
Next Post »

19 comments

comments
10 juillet 2016 à 21:16 delete

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

Reply
avatar
26 septembre 2017 à 03:54 delete

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

Reply
avatar
14 janvier 2018 à 12:39 delete

Error object not found how to fix it

Reply
avatar
14 janvier 2018 à 12:48 delete

Error object not found how to fix it

Reply
avatar
17 juin 2018 à 08:14 delete

Nice code. It works fine. Although I had to make some changes.

Reply
avatar
Anonyme
24 juin 2018 à 04:57 delete

Thank you. Your code solved my problem/

Reply
avatar
7 juillet 2018 à 04:47 delete

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

Reply
avatar
7 juillet 2018 à 04:50 delete

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

Reply
avatar
25 novembre 2018 à 09:45 delete

Can I hide the data and show only the search result? Also how can I use exact search instead of filter?

Reply
avatar
15 décembre 2018 à 13:27 delete

you saved my minor project!!!!!!!!!!
thank you so much!!

Reply
avatar
30 janvier 2019 à 01:23 delete

CONCAT allow you to join two or multiple character strings together

Reply
avatar
30 janvier 2019 à 23:10 delete

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

Reply
avatar
Anonyme
26 novembre 2019 à 09:51 delete

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

Reply
avatar
4 décembre 2019 à 16:04 delete

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

Reply
avatar
4 décembre 2019 à 16:18 delete

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;

Reply
avatar
5 octobre 2022 à 01:39 delete

NOT WORKING! IT CAN'T SEARCH OR FILTER!
I PASTE EVERYTHING TO MAKE SURE BUT IT STILL NOT WORK

Reply
avatar