How to filter data from a MySQL Database Table with PHP

php mysql filter results drop down
how to search and display data from database in php
how to fetch data from database in php using search button
how to search data from database in php and display in html table
search php mysqli
php mysql search form example
simple search code in php with demo
php code to retrieve data from mysql database and display

I am trying to code a search box that will search though a column in my database. If the search matches the column then that record will be printed in the table below.

I am searching on a column that contains the county of a company record. There are no errors being displayed however when I search for a county that is in the database the table remains blank. I can't see what I have done wrong in theory I think the code should work! Any help would be appreciated.

DBconnect.php

<?php
// connect to the database
$db = 'stylecraft_dev';
$host = 'localhost';
$user = 'stylecraft_admin';
$password = '000000';

$dbConn = mysql_connect($host,$user,$password) or die("Failed to connect to database");
$result = mysql_select_db($db, $dbConn) or die("Failure selecting database");
?>

form.php

        <?php
            $sql = "SELECT * FROM member ";

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

                $search_term = mysql_real_escape_string($_POST['search-box']);

                $sql .= "WHERE MB_COUNTY = '{$search_term}' ";
            }

            $query = mysql_query($sql) or die(mysql_error());
            ?>

            <form name="search_form" method="POST" action="stockists.php">
            Search: <input type="text" name="search_box" value=" "/>
            <input type="submit" name="search" value="Search the stockists...">
            </form>

            <table width="70%" cellpadding="5" cellspace="5">

            <tr>
                <td><strong>Company Name</strong></td>
                <td><strong>Website</strong></td>
                <td><strong>Phone</strong></td>
                <td><strong>Address</strong></td>
            </tr>

            <?php while ($row = mysql_fetch_array($query)) {?>
            <tr>
                <td><?php echo $row['MB_COMPANY'];?></td>
                <td><?php echo $row['MB_MOBILE'];?></td>
                <td><?php echo $row['MB_PHONE'];?></td>
                <td><?php echo $row['MB_COUNTY'];?></td>
            </tr>

            <?php } ?>
            </table>

You got a typo here

$search_term = mysql_real_escape_string($_POST['search-box']);

Must be according to your form

$search_term = mysql_real_escape_string($_POST['search_box']);

PHP Tutorials: How to filter data from a MySQL Database Table with , Hello everyone. This is another PHP / MySQL tutorial about how to filter data from a MySQL Duration: 19:38 Posted: Dec 19, 2012 private messaging using php and MYSQL database; PHP and mysql; Ifsc code search engine using php and mysql; Enter data in html textbox and show it in database ; Update and display database onchange using php,ajax,mysql,html; Urgent Need help PHP and MySQL stuff; How to copy data from one table to another table in SQL database using C#.Net; Php

Try:

            $search_term = mysql_real_escape_string($_POST['search_box']);
            $sql .= "WHERE MB_COUNTY LIKE '%".$search_term."%'";

The input name doesn't match - search-box versus search_box:

$_POST['search-box'] and <input type="text" name="search_box" value=" "/>

You should try to echo $search_term and $sql for debugging.

Filtering Records with MySQL WHERE Clause in PHP, how to filter the records from a MySQL database table using the SQL SELECT through passing it to the PHP mysqli_query() function to get the filtered data. You can search for data, filter by date and edit data on the same page without refreshing (reloading) page. AJAX technique is a fast technique for the process of the data between the PHP page and MYSQL database table.AJAX helps to update parts of a web page, without reloading the whole page. Display the data from the MySQL table and filter by

There is a space in the search box value.

<input type="text" name="search_box" value=" "/>

If that is not intentional for some reason and it is being submitted with search phrase then it may cause the search not succeeding, meaning if you are searching for "Mercedes" but submitting it with a space like " Mercedes" then it is not a match because of the space.

You can either trim the search_term $search_term = trim($search_term) or just remove that space from value=" ".

Php : How To Search And Filter Data In Html Table Using Php And , Php MySQL - Search Data In Mysql Database And Display It In Html Table Using MySQLI With Source Code. The user can select multiple countries from this dropdown list. On submitting the filter options, it will be posted to the PHP to process the MySQL data fetch based on the selected values. After fetching the results from the database, it will be listed below the filter.

This was your code $sql .= "WHERE MB_COUNTY = '{$search_term}' ";

The correct code is $sql .= " WHERE MB_COUNTY = '{$search_term}' ";

**space between " and where clause is needed **

How to filter data from a MySQL Database Table with PHP, You got a typo here $search_term = mysql_real_escape_string($_POST['search-​box']);. Must be according to your form $search_term  PHP MySQL WHERE Clause. In this tutorial you will learn how to select the records from a MySQL database table based on specific conditions using PHP. Filtering the Records. The WHERE clause is used to extract only those records that fulfill a specified condition. The basic syntax of the WHERE clause can be given with:

How to filter data from a MySQL Database Table with PHP, I suggest you echo the query, then run it against your database to see what happens. My query is that how to fetch column data from sql database Table and put that data into an string in PHP? Please check What I am doing. I have the table "DriveRegisterTable" in which 4 columns in database table, 1st "id" 2nd "username" 3rd "txtMail" 4th "rxtMail" .

Using WHERE to Filter MySQL Data, Previous · Table of Contents · Next. Sorting Data Retrieved from a MySQL Database, Advanced MySQL Data Filtering - AND, OR, NOT and IN  For this purpose first i will create a database with name allphptricks and then create a table in database with name sports and dump dummy data into it. Steps to Display Data From Database Into HTML Table Using PHP. Create a Database; Create a Database Table; Dumping Data into Table; Create a Database Connection Page; Create a Main Index Page; 1.

Using PHP drop down to filter MySQL table data? - PHP, Hey guys, since I've been told to drop MS access and use MySQL for a website based database, I decided to try it out and have been making some good  One of the most important requirements when retrieving data is to be able to filter that data so that the only rows returned are those which match specified search criteria. This is achieved in MySQL using the WHERE clause in conjunction with the SELECT statement.

Comments
  • Ah yes thankyou I have corrected this however this has not fixed my problem!
  • Can you put an echo in the first if isset(), that echos out the country? echo $_POST['search_box'] Check if it echos out the result you want. Then try manually querying it on phpmyadmin to see if it actually gives results.
  • @jonlloyd so right above the query, what does echo $sql give you? What if you copy that exact query and paste it in phpmyadmin.
  • When I go to run the query it produces an error I have not seen before! SQL query: Documentation $sql = "SELECT * FROM member "; MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$sql = "SELECT * FROM member "' at line 1
  • Here look at that it's not actually taking the value from $_POST search box. The query is not working because you got $sql in it as well. If you execute SELECT * FROM member it will work fine.
  • Ah yes thankyou I have corrected this however the problem persists!
  • Just noticed this was from 2 years ago. No idea how I ended up on this page.