PHP MySQL search database and display results

Bipsmedium
3 min readSep 11, 2021

--

Hi friends, in this tutorial you will learn how to perform a PHP MySQL search database and display results in a step-by-step process from scratch. We must know that database searching is very useful in any kind of application or in any dynamic website. Therefore, we can do this with the help of a PHP script and get the desired results based on the search input from HTML forms.

Steps to perform PHP MySQL search database and display results

Step 1:- Create a database in PHP Myadmin as shown in the below screenshot.

PHP MySQL search database and display results

Step 2:- Create a table in the database as shown in the below screenshot.

PHP MySQL search database and display results

Step 3:- Insert data in the table.

In this step, we will insert some data with the help of an SQL query so that we can search from these data and show the results later as shown below

PHP MySQL search database and display results
Query example for inserting data:- INSERT INTO employee_info (id, name, phone_no, age, department, created_at, updated_at) VALUES (NULL, 'ABC', '1234567891', '25', 'IT', current_timestamp(), '0000-00-00 00:00:00.000000');

Now, you can see the inserted data as shown below

PHP MySQL search database and display results

Step 4:- Create a PHP file and Make an HTML form

In this step, we will create an HTML form with a search element so that we can enter the search input as shown below

PHP MySQL search database and display results

Step 5:- Create a PHP file and establish the database connection as shown below

coonnect_test_db.php:-

Step 6:- Write a PHP script based on the search input.

Below are the two examples of searching data from the database

  • If the search input is matched with the column value and the column has a single value then it will return the single row as shown in the below screenshot.
PHP MySQL search database and display results
  • If the search input is matched with the column value and the column has more than one value then it will return multiple rows as shown in the below screenshot.
PHP MySQL search database and display results

Complete Code:- phpsearch.php

NOTE*

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

Also read, PHP MySQL ajax search autocomplete

CONCLUSION:- I hope this article will help you to understand the concept. If you have any doubt then please leave your comment below.

--

--

Bipsmedium

Hi, This is Biplab and I am a PHP laravel developer and other open source technologies. I am here to share my experience with the community.