How to Search a MySQL Table for Any Word or Phrase Using PHP



PHP


In this tutorial, we will show how you can search a MysQL Table for any word or phrase which you are looking for.

Say, if you are searching a database of various fruits and you want to see if they carry mangoes, so you look for the keyword 'Mango'. How can we create a PHP query to a MySQL table to only send back results of rows having the word 'Mango' in it.

This can be useful for many different types of applications. As an example, let's take Amazon.com. Amazon is a huge online retailer that sells thousands and thousands of different products. They have all of these products stored in a database. On amazon's home page is a search bar where a user can type in the item he or she is looking for. If the user searches for the term 'ipad', Amazon searches through all the items in its database and returns the products which contain the name 'ipad' in it. It then retrieves those items and displays them to the user.

So many other sites follow this format, including practically all retailers, so that customers can find the products they are looking for. Therefore, it has great application.

Coding

So how is this done with PHP and a MySQL database?

The general format of code to do this is:



Example

As an example to demonstrate this, below is a table of list of fruits:

Fruits