How to create a Search engine in PHP with MySQL

In this post I will provide you the source code of How to create a search engine in PHP. Beside this, above the source code you can see a video. If you are a beginner of PHP then I strongly recommend you to watch the video. The video will show you step by step guideline of how to use the source code. Hope all of you will enjoy it.
please follow the instruction given in our video tutorial

STEP:1
you have to paste the code below in to index.php

<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>

STEP:2
you have to paste the code below inside the search.php

<?php

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(!$button)
echo "you didn't submit a keyword";
else
{
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","your mysql username","password");
mysql_select_db("your database name");

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";

}

$construct ="SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website' 
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";

while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];

echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";

}
}

}
}

?>

About MD Mosleh Uddin

Trying to create an interesting and comfortable environment for the beginners of different programming languages and web design.

114 thoughts on “How to create a Search engine in PHP with MySQL”

    1. I will make more videos on php which will cover pagination and other necessary things. so keep in touch….. thanks for the comment

      1. excuse Mr Uddin i watched yo video and i d as you tought but i got an error like this[ Fatal error: Uncaught Error: Call to undefined function mysql_query() in E:\XAMP\htdocs\img\search.php:31 Stack trace: #0 {main} thrown in E:\XAMP\htdocs\img\search.php on line 31] please help mr i’m learning on this

  1. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a2739734/public_html/search.php on line 33

    please help

    1. I think you did some mistake with this section of the code. please check your code with this one. if your problem solved then let me know here. Thanks for your comment

      while($runrows = mysql_fetch_assoc($run))
      {
      $title = $runrows [‘title’];
      $desc = $runrows [‘description’];
      $url = $runrows [‘url’];

      echo "
      <a href=’$url’><b>$title</b></a><br>
      $desc<br>
      <a href=’$url’>$url</a>
      ";

      }

      1. <?php

        $button = $_GET ['submit'];
        $search = $_GET ['search'];

        if(!$button)
        echo "you didn't submit a keyword";
        else
        {
        if(strlen($search)<=1)
        echo "Search term too short";
        else{
        echo "You searched for $search “;
        mysql_connect(“localhost”,”root”,””);
        mysql_select_db(“search”);

        $search_exploded = explode (” “, $search);

        foreach($search_exploded as $search_each)
        {
        $x++;
        if($x==1)
        $construct .=”keywords LIKE ‘%$search_each%'”;
        else
        $construct .=”AND keywords LIKE ‘%$search_each%'”;

        }

        $construct =”SELECT * FROM search WHERE $construct”;
        $run = mysql_query($construct);

        $foundnum = mysql_num_rows($run);

        if ($foundnum==0)
        echo “Sorry, there are no matching result for $search.1.
        Try more general words. for example: If you want to search ‘how to create a website’
        then use general keyword like ‘create’ ‘website’2. Try different words with similar
        meaning3. Please check your spelling”;
        else
        {
        echo “$foundnum results found !”;

        while($runrows = mysql_fetch_assoc($run))
        {
        $title = $runrows [‘title’];
        $desc = $runrows [‘description’];
        $url = $runrows [‘url’];

        echo ”
        $title
        $desc
        $url
        “;

        }
        }

        }
        }

        ?>

        1. Notice: Undefined variable: x in C:\xampp\htdocs\search\search.php on line 21

          Notice: Undefined variable: construct in C:\xampp\htdocs\search\search.php on line 23

          Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search\search.php on line 32

          1. even i am having that error. were you able to solve your? if yes please reply me back. thank you

    1. It seems like you used the same code I provided in my post. It should work fine. So now I am sure that you have no problem in your source code. Can you please check your database. match your database table row with the code & let me know here

  2. hello..i tried bt m getting error on line 23 and 25..its showing declaration error..not declared x and construct..plzzzz tell me how to correct error…

  3. good job.is it possible that the search result look same as my website’s paritular division where it has image, div,html…etc.. actually i want to say may i put in the description box html or php as i use in my page such that looks may be better…or any other process? please ..

  4. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search.php on line 33

    Please tell me what this mean

  5. How do i fix this:
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search.php on line 33

    Please tell me what this mean

  6. i have a MSC. research about design search engine.i dont have time to build large database .is there any database for using?

  7. Sir This error r coming

    Warning: mysql_connect(): Access denied for user ‘u400683746_hus’@’localhost’ (using password: YES) in /home/u400683746/public_html/search.php on line 14 Warning: mysql_select_db(): Access denied for user ‘root’@’localhost’ (using password: NO) in /home/u400683746/public_html/search.php on line 15 Warning: mysql_select_db(): A link to the server could not be established in /home/u400683746/public_html/search.php on line 15 Warning: mysql_query(): Access denied for user ‘root’@’localhost’ (using password: NO) in /home/u400683746/public_html/search.php on line 30 Warning: mysql_query(): A link to the server could not be established in /home/u400683746/public_html/search.php on line 30 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/u400683746/public_html/search.php on line 32 Sorry, there are no matching result for search.

  8. Hi, I watch your HTML moving text tutorial on youtube. thank you. But I tried on my wordpress web-html (Text) widget, i it doesnt work.

    can you help me pls.

  9. hello ..
    i watched your tutorial Search engine in PHP with MySQL and done as it is what you have done.
    every thing is working..

    but,, i am getting Notice error like this

    Notice: Undefined variable: i in C:\xampp\htdocs\search\search.php on line 21

    Notice: Undefined variable: construct in C:\xampp\htdocs\search\search.php on line 23

    can u help me to solve this problem.

  10. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\new\search.php on line 33

    what is this?

  11. sir i have done every thing as u have told …….. but the result it is showing is ………….

    Sorry, there are no matching result for facebook.

    1. Try more general words. for example: If you want to search ‘how to create a website’ then use general keyword like ‘create’ ‘website’
    2. Try different words with similar meaning
    3. Please check your spelling

  12. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/u175524097/public_html/search.php on line 32 Sorry, there are no matching result for web developing ideas.

    I used your code according to your instruction but this error is shown when i insert a keyword and then search.
    Plz….!!! Help me…….!

  13. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

    i get this error when i try to search more that 1 word such as “space ship”
    but when i type 1 word like “space” there is no error.. please help me

  14. I want convert pdf to plain text, but the code what i get is still not work well..
    i need it for search string in pdf file using php..
    Please help me..
    thanks before..

  15. Hai Sir,

    Please can you help me to search the data like google search engine. That is i want to search the data not only search by the perticular field , search by the all fields in the table.

  16. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mywebsites\searcheng\search.php on line 34
    Sorry, there are no matching result for hi.

    1. Try more general words. for example: If you want to search ‘how to create a website’ then use general keyword like ‘create’ ‘website’
    2. Try different words with similar meaning
    3. Please check your spelling

  17. Notice: Undefined variable: construct in C:\xampp\htdocs\phptest\search.php on line 25

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\phptest\search.php on line 32
    Sorry, there are no matching result for bcsakj .

  18. hi ,,

    I got this error in the code why??

    Notice: Undefined variable: x in C:\xampp\htdocs\search\search.php on line 17

    Notice: Undefined variable: construct in C:\xampp\htdocs\search\search.php on line 19
    1 results found !

    Notice: Undefined index: description in C:\xampp\htdocs\search\search.php on line 37

  19. These are my question sir: Saving of picture to database and retrieving it again in php and mysql. (2) How generate random number alphabet like 7388826105DF in php or javascript. thanks sir in advance.

  20. Sir, your tutorial was very helpful . I have a query relating to this. I have studied that, spider is a program that automatically fetches Web pages. Spiders are used to feed pages to search engines. It’s called a spider because it crawls over the Web. Another term for these programs is webcrawler.

    Can I attach a spider program to my search engine. ? Please Explain how a spider program is created and how it is attached to our search engine. How it is possible?…

  21. Sir i used your code for searching images but its showing warnings…like

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘root’@’localhost’ (using password: YES) in C:\wamp\www\search.php on line 10

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search.php on line 28

    helpp

  22. Sir i Try search.php But it Not Works
    When i enter keyword in searchbar the feedback is full php script please help me

  23. plz delete the line no 33 and paste

    $query = “select * from tabled where field like \”%$trimmed%\”
    order by field”;

  24. Assalamu alaikum friends…

    I got this error

    Notice: Undefined variable: x in C:\xampp\htdocs\search\search.php on line 21

    Notice: Undefined variable: construct in C:\xampp\htdocs\search\search.php on line 23
    1 results found !

    answer: before foreach
    declare $x=” “;
    $construct=” “;

  25. User tables to create a database for example
    CREATE TABLE page (

    page_id int(10) unsigned NOT NULL auto_increment,

    page_url varchar(200) NOT NULL default ”,

    PRIMARY KEY (page_id)

    ) TYPE=MyISAM;

    CREATE TABLE word (

    word_id int(10) unsigned NOT NULL auto_increment,

    word_word varchar(50) NOT NULL default ”,

    PRIMARY KEY (word_id)

    ) TYPE=MyISAM;

    CREATE TABLE occurrence (

    occurrence_id int(10) unsigned NOT NULL auto_increment,

    word_id int(10) unsigned NOT NULL default ‘0’,

    page_id int(10) unsigned NOT NULL default ‘0’,

    PRIMARY KEY (occurrence_id)

    ) TYPE=MyISAM;

    You can complete the creation of lay

  26. You searched for learn php

    ( ! ) Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\search\Search.php on line 14
    Call Stack
    # Time Memory Function Location
    1 0.0420 138192 {main}( ) ..\Search.php:0
    2 0.0420 138488 mysql_connect ( ) ..\Search.php:14

    ( ! ) Notice: Undefined variable: x in C:\wamp\www\search\Search.php on line 21
    Call Stack
    # Time Memory Function Location
    1 0.0420 138192 {main}( ) ..\Search.php:0

    ( ! ) Notice: Undefined variable: construct in C:\wamp\www\search\Search.php on line 24
    Call Stack
    # Time Memory Function Location
    1 0.0420 138192 {main}( ) ..\Search.php:0
    1 results found !
    learn php
    php database
    http://www.google.com

  27. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a6957434/public_html/search.php on line 32

    help me sir search reasult is like this what to do ?

  28. Notice: Undefined variable: x in C:\xampp\htdocs\search01\search.php on line 21

    Notice: Undefined variable: construct in C:\xampp\htdocs\search01\search.php on line 23
    2 results found !

    please help me how can i solve it.

  29. I copy and followed all the step mention above however i got an error message..
    1. Undefined variable: x
    2. Undefined variable: construct
    3. Call to a member function query() on a non-object in

    I’m using wamp server and mysql version 5.6.17
    please make the code for mysqli version 5.6.17

  30. I’m using wamp server and mysql version 5.6.17
    please help me with source code for latest mysql version 5.6.17

    I tried all the above step however got an error message
    1. Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

    2. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C

    3.Undefined variable: x

    thanks in advance

  31. can you explain the part with if(!$button) if you put the exclamation mark before !$button that would make the condition false right so in essencce the part you didn’t submit a keyword” would never execute and the code would go to execute the else part right. can you explain what value is being read in $button = $_GET [‘submit’];

    $button = $_GET [‘submit’];
    $search = $_GET [‘search’];

    if(!$button)
    echo “you didn’t submit a keyword”;
    else
    {
    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”your mysql username”,”password”);
    mysql_select_db(“your database name”);

    $search_exploded = explode (” “, $search)

  32. really thanks for ur website.
    sir how i can make mysql username and its password?
    mysql_connect(“localhost”,”your mysql username”,”password”);

  33. I would like to use the search engine without the data base. I just want the search engine to search my whole site and give the results. My site is not complex enough to use a data base. Also how can I make the search engine just search a single page in my site and give the results.

  34. Bro I want to create a search engine using PHP with a new innovation idea.
    pls connect with me in Facebook , I have send friend request to u my ID is /ruvaid.ahmed.1

  35. hai..i followed the same step from your video but it shows the following error.

    “Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\p2\search.php on line 32
    Sorry, there are no matching result for click.”

  36. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\search.php on line 33

    This comes please help I am stuck with this
    It is very urgent

  37. sir i ll use those code but i am not able to upload the photo i can also create a image file at xamp/htdoc but i dontkno why the image is not shown properly after uploading . it can shown unexpected value token is shown in the line ()

    PHP Javascript Image Upload

    function updatepicture(pic) {
    document.getElementById(“image”).setAttribute(“src”,pic);
    }

    PHP Javascript Image Upload

    Upload message will go here.

    THIS IS A DB SUPPORT FILE
    0){
    if($_FILES[‘file’][‘size’]

    parent.document.getElementById(“message”).innerHTML=””;
    parent.document.getElementById(“file”).value=””;
    window.parent.updatepicture(“”);

    parent.document.getElementById(“message”).innerHTML=”There was an error uploading image .Please try again later.”;

    parent.document.getElementById(“message”).innerHTML=”Your file is larger than 100kb. Please choose a diffrent picture.”;

    }
    }
    <?php
    }
    }else{

    }

  38. sir,
    i am having problem ,when i clik on links that are result of query ,i am not redirected to page and link is not working

  39. Hello sir , i try to run your code , but there would some error . could you check it for me .

    this is the code from yours and i edit it to synchronize with my database . I couldnt figure what was error said about . could you help me .

    //THE CODE PHP
    <?php

    $button = $_GET ['submit'];
    $search = $_GET ['search'];

    if(!$button)
    echo "you didn't submit a keyword";
    else
    {
    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”root”,”admin”);
    mysql_select_db(“database”);

    $search_exploded = explode (” “, $search);

    foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1)
    $construct .=”keywords LIKE ‘%$search_each%'”;
    else
    $construct .=”AND keywords LIKE ‘%$search_each%'”;

    }

    $construct =”SELECT * FROM user WHERE $construct”;
    $run = mysql_query($construct);

    $foundnum = mysql_num_rows($run);

    if ($foundnum==0)
    echo “Sorry, there are no matching result for $search.1.
    Try more general words. for example: If you want to search ‘how to create a website’
    then use general keyword like ‘create’ ‘website’2. Try different words with similar
    meaning3. Please check your spelling”;
    else
    {
    echo “$foundnum results found !”;

    while($runrows = mysql_fetch_assoc($run))
    {
    $title = $runrows [‘title’];
    $desc = $runrows [‘description’];
    $url = $runrows [‘url’];

    echo ”
    $title
    $desc
    $url
    “;

    }
    }

    }
    }

    ?>

    //this is the error shown

    Notice: Undefined variable: x in C:\xampp\htdocs\ui\uitmtry\index.php on line 22

    Notice: Undefined variable: construct in C:\xampp\htdocs\ui\uitmtry\index.php on line 24

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ui\uitmtry\index.php on line 33
    Sorry, there are no matching result for siti.

    1. Try more general words. for example: If you want to search ‘how to create a website’ then use general keyword like ‘create’ ‘website’
    2. Try different words with similar meaning
    3. Please check your spelling

  40. How to make it with advanced fields like search date range (I.e. from date 23/07/2015 to 25/07/2015 and between city (i.e. Dhaka to New York) and radio button field like select male and female,etc.

    Where the result will be displayed. What code to put for display result code. If i want to give the style to search result.

  41. dude the code works and I thank you for solving my problem GOOD STUFF :)
    However I do have one issue.

    foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1)
    $construct .=”keywords LIKE ‘%$search_each%’”;
    else
    $construct .=”AND keywords LIKE ‘%$search_each%’”;

    }

    I AM TRYING TO ADD TWO OTHER KEYWORDS TO THIS STATEMENT AND I GET A BLANK PAGE ALSO IF I REPLACE THE SECOND STATEMENT WITH ANOTHER WORD FROM THE DATABASE LIKE “subcategories” IT DOESN’T RECOGNIZE THE KEYWORDS IN THE COLUMN, ANY HELP WITH THIS. AN “AND ” “OR ” STATEMENT DOESNT WORK EITHER

  42. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/u315213894/public_html/search.php on line 10

    Why This Come Please Help Me To Solve This

  43. How can I make a webpage open instead of a link when I search the database. For example if i went to macys.com and type shirt a webpage with shirts come display instead of a link Thank You

  44. Thanks for this sir!it helps a lot in my project.my only question is what if in the index.php i have 2 select option,each select option has 2 diff name and the third input type is date.total of 3 name in all.how to declare that in the search.php with diff name.

    1. i’m sorry mr Salahudin for me it brings error like[Fatal error: Uncaught Error: Call to undefined function mysql_query() in E:\XAMP\htdocs\img\search.php:31 Stack trace: #0 {main} thrown in E:\XAMP\htdocs\img\search.php on line 31]

  45. Hello Sir,
    I watched your video and tried to make at my end. Bu when I am clicking on search button on index.php page it is showing content of search.php rather than the result that i want to look for. Please find the solution of this.
    Thanx

  46. Dear,

    I would like to build search engine for only blog sites. Kindly help if this can be done the way you have suggested or any way i could integrate Builtwith api?

    Regards

  47. sir, I want the code for search engine with crawling, parsing, indexing and page ranking in php and mysql. please provide me the code..

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × three =