How to Create a Custom Search Engine in PHP & MySQL?

By | June 7, 2013
search engine in PHP

I love to play with PHP codes and this is one of my favorite activities for last few years. I created this blog for sharing video tutorials with you about web development, web designing and SEO etc. Before this tutorial I published a tutorial on how to create a calculator in PHP, also how to create a converter in PHP and complete PHP video tutorials on this blog. A registration form was also there for you. So now let me tell that through today’s tutorial you’ll learn how to create a simple search engine in PHP. When someone will enter a keyword or phrase of keywords then this search engine will be fetching the data from the MySQL database and will be displaying on the browser. So you’ll get the basic idea of creating this thing very easily. However you can wait for my Advance search engine tutorial for a website.

Video tutorials for creating search engine in PHP

There are three video tutorials by watching which you can easily learn how to create a simple search engine in PHP. The source code will also be shared with you below the video tutorials. Also I’ll give you the steps for creating the search engine throughout this article.
The first thing is first and which is to open NotePad or Notepad++ to start writing the basic HTML structure and then save the file with name “search.php”, so this will be your display file for the users. You just need to insert a form tag and the form method will be “get” and action will be “search.php”. Insert two input fields inside your form tag, one field for text value and the second field is for submit button. That’s all for the HTML. Your Code should be looking like this:
<html>

<head>
<title>Search Engine in PHP</title>
</head>
<body>
<form action=”search.php” method=”get”>
Search Engine:<input type=”text” name=”value” placeholder=”Search Anything here”>
<input type=”submit” name=”search” value=”Search Now”>
</form>
<hr>
</body>
</html>

Now having the HTML work done, create a database using PHPMyAdmin in localhost with any name you like, inside the database create a table with the name “sites”, and put 5 fields for following data to be inserted:
  • Site_ID
  • Site_title
  • Site_link
  • Site_keywords
  • Site_description
Just make the site_ID as auto increment and assign “Primary Key” to it. And save the Table. Now you can insert some sites’ data into your tables for displaying on the web browser when someone will search using the search box we already created.
Now watch below video tutorials to understand everything in a more shining way. I’ll provide you the PHP source code below the videos:
Creating Search Engine in PHP Video #1
Creating Search Engine in PHP Video #2
Creating Search Engine in PHP Video #3
Now when you’ve created the table ready and inserted some data as guided in the videos, then here is the PHP script which will make the search engine to work, here is the script:

<?php
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“my_db”);
if(isset($_GET[‘search’])){
$search_value = $_GET[‘value’];

$query = “select * from sites where site_keywords like ‘%$search_value%'”;
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$title = $row[‘site_title’];
$link = $row[‘site_link’];
$desc = $row[‘site_desc’];
echo “<h1>$title</h1><a href=’$link’>$link</a><p>$desc</p>”;
}
}
?>

This is a really really simple script and I hope even a kid can understand it. Now let’s make all the things work together. Here is the full code of HTML & PHP altogether:

<html>
<head>
<title>Search Engine in PHP</title>
</head>
<body>
<form action=”search.php” method=”get”>
Search Engine:<input type=”text” name=”value” placeholder=”Search Anything here”>
<input type=”submit” name=”search” value=”Search Now”>
</form>
<hr>
<?php
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“my_db”);
if(isset($_GET[‘search’])){
$search_value = $_GET[‘value’];

$query = “select * from sites where site_keywords like ‘%$search_value%'”;
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$title = $row[‘site_title’];
$link = $row[‘site_link’];
$desc = $row[‘site_desc’];
echo “<h1>$title</h1><a href=’$link’>$link</a><p>$desc</p>”;
}
}
?>
</body>
</html>

Now what do you think of this simple custom search engine. Kindly share your views in comment section, and also let me know what more you want to learn in PHP, so I can prepare myself for that.
If you liked my tutorials then kindly give me a favor by sharing this site on all social networks and also follow this blog. I’ll publish advance PHP tutorials soon. Take Care Buddies.

9 thoughts on “How to Create a Custom Search Engine in PHP & MySQL?

  1. Hafeez Khan

    Thanks Wali Bro see you after long time. well its indeed a very nice post for PHP lover like us. Wali bro php widget is working inside blogger template. Thanks and hope for earlier reply.

    Reply
    1. Wali Khan

      Welcome back Hafeez,

      PHP widgets won’t work inside Blogger, because blogger code is written in a strict XML language, which is itself a server side language. PHP is not directly accepted in blogger.

      Reply
  2. waqas malik

    nice tutorial just one problem if no result found than?
    else statement is not put in the script
    UNEXPECTED ELSE error occurs please edit source code you post in above thank you

    Reply
    1. Wali Khan

      Hello brother, if you have observed there should have been no result found condition then you may add that yourself very easily. Best luck.

      Reply
  3. waqas malik

    sir if you noticed search field is empty and click search button warning must be shows write some thing E.g but it passes through without any warning and showed all result……

    Reply
  4. Bangladesh Film Industries

    oh! yeah your are great man . your video is very good i learn from your video . it’s so beautiful . i am pray for you, allah long life to you for extra ode nary service ok not more to day thank you!

    Reply
  5. free classified

    how to search from 10 table. lke fb give suggetion. user start from a, group start from a,pages start from a ext. How echo hash images. jy images store by hash name. no .jpg or anything.
    when user upload images it save in drive as id-hashid.jpg how echo. one image but info from 3 rows.

    Reply
  6. pankaj

    I’m very pleased to find this page. I wanted to thank you for your time due to this fantastic read!!

    I definitely liked every part of it and I have you book marked to look at new
    stuff on your site.

    Reply

Speak Your Mind