
Building a Custom Search Engine using PHP and MySQL
Technologies used: PHP, HTML, MySQL, phpMyAdmin, Windows 10
While working at EyeRadar for the past couple months, one of the coolest skills I learned was creating my own search engine from scratch. Since every price comparison website needs a search engine to get the user’s favorite products and brands, I learned how to build a search engine that runs through a MySQL database and extracts the desired content. In this tutorial, I’m gonna show you how to create a simple search engine for a database. To check out this project on GitHub, click here.
For the sake of simplicity, I will be strictly focusing on HTML, PHP and SQL and ignoring CSS; we can always come back and make it look fancy later!
Create Search Engine Form in HTML

Note the form attributes and text box attributes of the HTML form:
- action= “search.php” — the form’s action is the end goal destination. This will be the location of the PHP script that is going to do all the searching.
- method=“get” — the GET method type is what will take the text entered into the text box and throw it into the url for the user.
- input type=“text” — the “text” type is simply going to do that, make the input a text input.
- name=“search” — the “name” can be whatever you want it to be.
- input type=“submit” — the “submit” type makes the form submit the search input to the script.
Now that our HTML form is set up, we can see our basic search form displayed below:

Create Search Engine Database (using MySQL/phpMyAdmin)
Next we will setup the back-end SQL database. Since our search engine is pretty simple, our MySQL database will also be simple. Just a few fields to store our data. To create the database and table, I will be using phpMyAdmin to manage the MySQL database backend. To learn more about Apache, MySQL and PHP setup, click here.
The image below is an example of a table from a database provided by MySQL. Create your database however you would like; create a table within your database and then add data to the table.

Now that the front-end and the database are ready, we can move onto the fun part of the actual search engine scripting. First we need to get the search input that the user searched for and format them for our use. Then we can connect to the database and run the query. And lastly we can display the search results back to the user.
Get Search Engine Keyword

We start by creating some variables that we will rely on for the script.
$search — stores the search input of the user.
$button — stores input type of search button.
Connect to MySQL Database and Process Query
Next, connect to MySQL using the mysqli_connect function. The function will connect you to the database of your choice. Then, run the query string against the database. We also get the number of rows returned from doing so. Using the number of rows, you can use conditionals to determine if you have search results to display to the user.
Display Search Engine Results to User

Now that we have all of our database results we can start displaying those results to the user. For the ease of the HTML, we will display 5 attributes of a product: name, brand, url, vendor and price. Using a while loop, you are able to parse through each and every one of the results returned from our search query we made earlier. The $runrows variable stores each individual record on each pass though of the loop. These results are stored as an associative array that we can process or display. Basically what this means is that we will be able to handle/format/display each of the results that are returned on a one-by-one basis. Each being stored in the same $runrows variable.
Using the Search Engine
Finally, let’s try using our search engine! I want to search for Gucci products so I typed in “gucci” and here are my results:

And that’s all, you’ve finally created your very own search engine! Stay tuned for more posts like this one in the future!