How to Create a Searchable Database Using MySQL & PHP



PHP


In this tutorial, we will show how you can create a search box for searching a MySQL database and then using PHP to send back matching queries for the search term we entered.

Type a query below in the search box to test out how this works.

The search database has the keywords 'car', 'helicopter', 'truck', 'lego', 'train', 'puzzle'. Search the box for any of these keywords and see the products which appear.

Say, if you are searching the database below of various toys, so you look for the keyword 'car'. Only the selections of toy items that contain the word, 'car' will appear as output.

This is how a search box works for a database table.

Below is a table of list of toys for kids:

Toys

Search Toy Database below:



Search the toy database above. This database contains different types of toys for boys, including cars, helicopters, and drones.

To test it out, enter 'car', 'helicopter', 'truck', 'puzzle', or 'lego' to see what happens.







HTML Code

The following is the HTML code needed to create this searchable database:



PHP Coding

The following code is the PHP code needed to create this table above:

We will explain this code in the following paragraphs below.

This is the original table for the complete database table of the Toy database items:

MySQL Table

The table above has the following rows, ID, images, Types, and Price.

The portion of code in the above PHP code which queries the MySQL table for the search term we enter is:



Because we are querying the table for the type of chips, we use the WHERE clause to search the column, Type. In this column, Type, we use the LIKE keyword to look for the phrase which the user enters. Because we saved the data the user entered in the search in the $searchterm PHP variable, we reference this variable in order to see if there are any matching queries.

The general format to search a My SQL table using PHP is:



Using this code, we can search any column of any table for any search term we are looking for.

Related Resources

How to Insert Images into a MySQL Database Using PHP
How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP




HTML Comment Box is loading comments...