PHP Tutorials: How to filter data from a MySQL Database Table with PHP




Hello everyone. This is another PHP / MySQL tutorial about how to filter data from a MySQL Database Table using PHP. As always, i have to apologize for my …

source


26 responses to “PHP Tutorials: How to filter data from a MySQL Database Table with PHP”

  1. I have got an error please help

    You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= '3'' at line 1

    I am using the same code as you have shown.

    <?php include("connection.php")?>

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

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

    {
    $search_term=trim(mysql_real_escape_string($_POST['search_box']));
    $sql .= "WHERE gr_no= '{$search_term}' ";

    }

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

    <html>
    <body>

    <form action="grinfo.php" name="search_form" method="POST">
    <input type="text" value="" size="20" name="search_box">
    <input type="submit" name="search" value="GR SEARCH">
    </form>

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

    <tr>
    <td><strong>ID</Strong></td>
    <td><strong>Truck</Strong></td>
    <td><strong>Driver</Strong></td>
    </tr>

    <?php while($row=mysql_fetch_array($query)){?>
    <tr>
    <td><?php echo $row['gr_no'];?></td>
    <td><?php echo $row['gr_truck'];?></td>
    <td><?php echo $row['gr_driver'];?></td>
    </tr>

    <?php } ?>
    </table>

    </body>
    </html>

  2. i m using this code with select tag…but it's not filtering
    which name should i give in" $search_term=mysqli_real_escape_string($c,$_POST['stock']);"
    stock is the name give to select tag…pls tell me the solution

  3. hi please reply soon………………..
    i am getting the below error

    Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in E:XamphtdocsAyeshadisplay_data.php:21 Stack trace: #0 {main} thrown in E:XamphtdocsAyeshadisplay_data.php on line 21

    the below is my code:

    <?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "phonebook";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT * FROM employees ";
    $result = $conn->query($sql);

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

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

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

    }

    ?>
    <form name = "search_form" method= "Post" action="display_data.php">
    Search : <input type = "text" name= "search_box" value="" />
    <input type = "submit" name = "search" value= "Search the table…">

    </form>
    <form>
    <table width="100%" border="1px">
    <?php
    echo "<tr>
    <th>Emp ID </th>
    <th> First Name</th>
    <th> Last Name </th>
    <th> Designation</th>
    <th>Extn</th>
    <th>Mobile No.</th>
    <th>Email</th>
    <th>skype ID</th>
    </tr>";
    if (mysqli_num_rows($result)>0) {
    // output data of each row
    while($row = mysqli_fetch_array($result)) {

    echo "<tr>";
    echo " <td> " . $row["EmpID"]. " </td> <td> " . $row["FirstName"]. "</td><td>".$row["LastName"]." </td> <td>".$row["Designation"]." </td><td>".$row["Extn"]." </td><td>".$row["MobileNo."]." </td><td>".$row["Email"]." </td><td>".$row["skypeID"]. "</td>";

    echo "</tr>";

    }
    } else {
    echo "0 results";
    }
    echo "</tr>";
    ?> </table>
    </form>

    <?php
    $conn->close();
    ?>

  4. your tut helped me lot bro, thank you .
    I just want to know little change in this tut .

    Here it is with three columns 1.id 2.name 3.level and i want to add one more column named "experience" .

    So i want to get the 'level' and 'experience' from the user so that database will give away the particular name and id corresponding to the 'level' and 'experience' ..

    please me ..

  5. Seems like a brilliant guide. Followed it however at the very end I get an error.

    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 '= 'Snitch'OR Prod_ID = 'Snitch'' at line 1.

    Any ideas anyone?

  6. Thank you so much TheEndritv, I appreciated for the nice tutorials that you put on the Youtube….
    I am working on student project and I need help for student table…..
    1: I created table that stores student, course table that stores courses, and Junction table which gets student information from student table and courses from course_ID table….. I created form that I can easily display all the student….. but the issue that I am facing is , I want to select the course _ID from combobox or drop_down box , then I want to display only students who are in that course_ID . I created dropdown box and I can see all the Cours_IDs but when I select Course_ID I want to see students informations….. I am using only PHP,HTM , and SQL.

  7. what if you don't need that search button? All you have to do is to type the characters or word you were searching in the textbox and the table will change depending on the characters of the textbox. How will i do that sir?

  8. suggestion please ….. 1) how do i suppress the form rows. I just need the first two rows printed. OR suppress the entire form rows and show only the result after a search has been made,,,,?

  9. sorry i mean the error comes after $query = mysqli_connect($sql) or die(mysql_error());
     Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
    what should i do

  10. hello your tutorial is nice but i keep getting this error while trying to connect to my db.
    Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead. what can i do.

  11. Hi, the code work fine but if i want doing multiple it refer to previous table.can you help me. I change textfield to the dropdownlist>

    <form action="" method="post" name="form1">
     <select name="search_box" class="form-control">
    <option value="">All Location</option>
    <?php
    $strSQL = "SELECT DISTINCT neighbourhood FROM reserve";
    $objQuery = mysql_query($strSQL);
    while($objResuut = mysql_fetch_array($objQuery))
    {
    ?>
    <option value="<?php echo $objResuut["neighbourhood"];?>"><?php echo $objResuut["neighbourhood"];?></option>
    <?php
    }
    ?>
     </select>
     <br>

    <input type="submit" name="search" value="Search table" />
                                               
    </form>

    <form action="" method="post" name="form1">
     <select name="search_box1" class="form-control">
    <option value="">All Sport</option>
    <?php
    $strSQL = "SELECT DISTINCT category FROM reserve";
    $objQuery = mysql_query($strSQL);
    while($objResuut = mysql_fetch_array($objQuery))
    {
    ?>
    <option value="<?php echo $objResuut["category"];?>"><?php echo $objResuut["category"];?></option>
    <?php
    }
    ?>
     </select>
     <br>

    <input type="submit" name="search" value="Search table" />
                                               
    </form>

    $sql="SELECT * FROM reserve" ;

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

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

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

    }

    $sql="SELECT * FROM reserve" ;

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

    $search_term1 = mysql_real_escape_string($_POST['search_box1']);

    $sql .= " WHERE category = '{$search_term1}'";

    }

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

    ?>

Leave a Reply