All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP MySQL Search Database And Display Results

Last Updated : Mar 11, 2024

PHP MySQL Search Database And Display Results

In this article we will show you the solution of PHP MySQL search database and display results, PHP MySQL search database & display results is a procedure for searching for particular information from the a MySQL database table depending on user input, and afterwards displaying the results in an approachable format using PHP computer language.

Typically, a search form on a web page is used by the user to input the search term.

Now let us talk about the PHP mysql search database and how it can display results.

Step By Step Guide On PHP MySQL Search Database And Display Results :-

The user's search query is then supplied to a PHP script which connects to the MySQL database & runs a SELECT statement using the LIKE operator to search the appropriate table for rows that match the user's query.

After that, using HTML and CSS, the results are presented to the user in a way that is visually pleasing.

This method is frequently used in web applications that search for and show data like user profiles, product listings, and news items.

<?php
// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Retrieve the user's search query
$search = $_POST['search'];
// Build the MySQL SELECT statement with the LIKE operator
$sql = "SELECT * FROM products WHERE product_name LIKE '%$search%'";
// Execute the SELECT statement and store the results in a variable
$result = mysqli_query($conn, $sql);
// Check if any rows were returned
if (mysqli_num_rows($result) > 0) {
    // Output the results in an HTML table
    echo "<table>";
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>" . $row['product_name'] . "</td><td>" . $row['description'] . "</td><td>" . $row['price'] . "</td></tr>";
    }
    echo "</table>";
} else {
    // Output a message if no rows were returned
    echo "No results found.";
}
// Close the database connection
mysqli_close($conn);
?>
  1. You can see that we have written PHP code here that search a MySQL database for items which match a user's search query & shows the results in an HTML table.
  2. The mysqli connect() method is used in this code to first create a connection to the MySQL database.
  3. After that, we use the $_POST variable to obtain the user's search term from a form.
  4. Using the LIKE operator, we construct a MySQL SELECT statement to look for products whose names include the user's search input.
  5. The mysqli query() function is used to carry out the SELECT statement, and the output is saved in a variable.
  6. The mysqli num rows() method is then used to determine whether any rows were returned.
  7. If records were returned, using the mysqli fetch assoc() method, we loop through each row and print the product name, description, & price in an HTML table.
  8. We output the message "No results found" if no rows are retrieved.
  9. Lastly, we use the mysqli close() function to terminate the database connection.

Conclusion :-

As a consequence, we have successfully learnt how to use PHP to search a MySQL database and display the results.

We also discovered that the PHP script, which searches a MySQL database for items which match a user's search query & shows the results in an HTML table, is a helpful tool for web developers to give a quick and simple way for users to search for certain information from a database.

I hope this article on PHP MySQL search database and display results helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪