PHP & MySQL Tutorial | Pagination in Search Engine

Posted on Posted in PHP

For better understanding of the source code please watch the video first.

In this Post I will discuss about pagination in Search Engine using PHP & MySQL. I will use my search engine source code & add pagination to that script. So you can have a look at How to create a search engine in PHP first. That will definitely help you to understand easily.

Please create two page first. One is “index.php” & another one is “search.php”.
Then follow the steps written below:

At first create a table inside your database. create 4 columns inside that table. In my script I named those column as “id”, “title”, “description”, “url” . For “id” choose INT type, index it as PRIMARY & choose auto increment. For other fields you can simply choose the type as TEXT. Once you are done with the database then follow the instructions given below:

please paste this code inside index.php

STEP: 1

<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>

Then, please add this code inside search.php

<?php
   
$button = $_GET ['submit'];
$search = $_GET ['search']; 
 
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("mysql_host_name","mysql_username","mysql_password");
mysql_select_db("database_name");
   
$search_exploded = explode (" ", $search);

$x = "";
$construct = "";  
   
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
   
}
 
$constructs ="SELECT * FROM database_table_name WHERE $construct";
$run = mysql_query($constructs);
   
$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>";
 
$per_page = 1;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$getquery = mysql_query("SELECT * FROM database_table_name WHERE $construct LIMIT $start, $per_page");
 
while($runrows = mysql_fetch_assoc($getquery))
{
$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>
";
   
}
 
//Pagination Starts
echo "<center>";
 
$prev = $start - $per_page;
$next = $start + $per_page;
                      
$adjacents = 3;
$last = $max_pages - 1;
 
if($max_pages > 1)
{   
//previous button
if (!($start<=0)) 
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    
         
//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}
                         
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                 
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}
                                 
}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}
         
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
}   
echo "</center>";
} 
} 
?>

Books you should read :

PHP and MySQL in easy steps
Practical PHP and MySQL Website Databases: A Simplified Approach (Expert's Voice in Web Development)
PHP & MySQL: The Missing Manual

About MD Mosleh Uddin

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

223 thoughts on “PHP & MySQL Tutorial | Pagination in Search Engine

      1. Parse error: syntax error, unexpected ‘then’ (T_STRING), expecting ‘,’ or ‘;’ in C:\xampp\htdocs\search\search.php on line 36

    1. man i got the search engine part working but when i go to search i get this error
      You searched for google

      ( ! ) Warning: mysql_connect(): Access denied for user ‘mysql_root’@’localhost’ (using password: YES) in C:\wamp\www\search.php on line 10
      Call Stack
      # Time Memory Function Location
      1 0.0010 148888 {main}( ) ..\search.php:0
      2 0.0010 149504 mysql_connect ( ) ..\search.php:10

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

      ( ! ) Notice: Undefined variable: construct in C:\wamp\www\search.php on line 19
      Call Stack
      # Time Memory Function Location
      1 0.0010 148888 {main}( ) ..\search.php:0

      ( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\search.php on line 28
      Call Stack
      # Time Memory Function Location
      1 0.0010 148888 {main}( ) ..\search.php:0
      2 0.0059 156424 mysql_num_rows ( ) ..\search.php:28
      Sorry, there are no matching result for google.

      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

      why is this?

      1. Build a search engine for a small text dataset called NSF_awd_2003_00. This dataset contains some text files, each of which is a description of a NSF funded project in year 2003. Each description includes Title, date, Award Number, Abstract….
        In order to build a search engine on this data set, you need to go through the following steps:
        1. Pre-process this data for the following purposes:
        – Create an Associative Array (Map). Each record is for one project. The key of each record is the award number, and the value is string contains all other information of the project.
        – Prepare input files for the MapReduce job that generates inverted index for project abstracts. Each input record is like of following:
        0300005 This project will analyze the newly released …
        In other words, each record contains a key which is the award number, and a value which is the abstract text.

        2. Implement a MapReduce job to create an inverted file for abstracts.

        3. Load the inverted file generated in step 2 and the associative array generated in step 1 in to memory data structures, based on which create a simple search engine.

        Please i need help

  1. can you please tell me the code that is in connect.php
    so i can copy and past it, and is connect.php important ? for search engine to work properly? if you answer me this would make life easeier

  2. I LIKED YOURE PAGE ON FACEBOOK________can you please tell me the code that is in connect.php
    so i can copy and past it, and is connect.php important ? for search engine to work properly? if you answer me this would make life easeier
    .

    1. well, connect.php is not important for the search engine to work properly. I have added the code for connecting with a database in line no 11 & 12. If you modify the code with your mysql database information then your search engine will work perfectly. But it is not a good process of writing the code for connecting with a database. so I recommend everyone to write the database connection code in another page & include that page to the search.php
      please watch this post first. http://www.findsourcecode.com/php/php-mysql-how-to-connect-with-a-database/
      I have shown there how to create a database and how to connect with it. So once you finish creating the connect.php then delete line 11 & 12 from search.php & add the following code at the top of search.php
      < ?php include ("connect.php"); ?>
      Thanks for your comment.

    1. wa alaikum as salam. inside your database table you must have column like ‘title’ as my search.php code is searching for the title column. Have a look at line no 15~23. you can change this portion to match with your table column.

  3. my brother in islam , is it ok if i give you my webhosting and server information to log in and you would do that for me , and allah may reward you. I trust you becasue you are a brother, please reply to me, as i cant do it myself because i am lost.

    1. If you think that it is ok for you to give me those confidential information then I believe in shaa Allah I will try my best to help you.

  4. Everything works correct ….. but my database will have more then 500.000 exaples…. how do you limit the pagination ? like > ( prev.. 1 2 3 4 5 6 ..100 next ) <

    1. @Tasos Ive been trying a while to limit the pagination and still unsuccessful.. have you found a solution yet.. if so please share your knowledge.. like wise if i find a solution i will post it to you…my email adderss is:

      daddy1@hotmail.co.uk

      Feel free to email me with any questions or may be we can work on it together? so far i have managed limit the search to 10 pages,nevertheless, when its on the 10the page it increments by 10 more pages and vise versa, but does not show the current page numbers below the search after the 10th page…on the other hand the current pages beyond the 10 pages can be seen on the URL..

    2. I have modified the code. have a look from line 61 to 154. I hope now you will have limited pagination. Thank you for your comment.

    1. @ Mish Abesadze Your can add image to your results. i did it by concatanation as shown below..

      I only need to limit pagination to compleate the search engine.. can you help?

    2. You can add images to your search results quite easily. In you search table add an image field using Var (255) and put in the name of your picture including file extension.

      Be sure to add the to the search script the filed name in the php script to call in the image

      row 052: $image = $runrows [‘image’];

      Then store all of your images in a directory (searchimages) and than add an echo command to your results area

      <img src="searchimages/ height=”” width=””/>

      I have done exactly the same thing in a Child and Family internet Search Engine that i am working on.

      http://www.searchsafekids.com

      If you have already worked it out, then no worrries, but if you want any more help just let me know and ill here where is can.

      many thanks

      Mark

      1. I have modified the code. have a look from line 61 to 154. I hope now you will have limited pagination. Thank you for your comment.

  5. @Misha Abesadze…

    $start = $_GET[‘start’];
    $max_pages = $foundnum / $per_page;
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM videos WHERE $construct LIMIT $start, $per_page”);

    while($runrows = mysql_fetch_assoc($getquery))
    {
    $thumbs = $runrows [‘thumbs’]; Z-|
    $title = $runrows [‘title’]; Z-|
    $channel = $runrows [‘channel’]; Z-|
    $url = $runrows [‘url’]; Z-|

    echo ‘ ‘;

    ————————————————————————————–
    I just made it fast for you….. only you need to do create in you database table named videos…. and Z-| are you rows in the database …. you are done.

    DATABASE :

    In row thumbs you need ( http://findsourcecode.com/pagination/website.jpg or someting like that.. only image url..

    title = title
    channel = channel…… categories or what erer you like

    AND url = url …. the main url.

    if you want only to display images…

    delete <a href=……………. and keep <img search …..bla bla bla..
    ———————————————————————————–

    You are finish……

    NOW I NEED FROM SOMEBODY HELP HOW TO SHORTEN THE PAGINATION NEVER EVER I HAD TO SHORTEN A PAGINATION SO PLEASE HELP..

    1. I created column in database, type BLOB, but when results show there is �PNG  IHDR��a�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>������� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-���"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~{�-�]c�K’Xt�����o��(�h���w��?�G�%�fI�q^D$.Tʳ?
      this text instead image

      1. Place the ( script here )… and the images url …like that below .
        —————————————————————————————
        http://www.example.com/image.jpg or something else i will try to solve it for you.
        —————————————————————————————

  6. $x++;
    if($x==1)
    $construct .=”title LIKE ‘%$search_each%'”;
    else
    $construct .=”AND title LIKE ‘%$search_each%'”;

    on line 17 the browser tell me the its undefined.. i followed all your instructions and was unsuccessful.. please help.. this is my version of your code..

    <?php

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

    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”shuel”,”botlamiah1″);
    mysql_select_db(“project_db”);

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

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

    }

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

    $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 !”;

    $per_page = 1;
    $start = $_GET[‘start’];
    $max_pages = $foundnum / $per_page;
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM products WHERE $construct LIMIT $start, $per_page”);

    while($runrows = mysql_fetch_assoc($getquery))
    {
    $id = $runrows [‘id’];
    $product_name = $runrows [‘product_name’];
    $details = $runrows [‘details’];

    echo ”
    $Product_name
    $details
    $details
    “;

    }
    echo “”;
    $prev = $start – $per_page;
    $next = $start + $per_page;

    if (!($start<=0))
    echo "Prev“;

    $i=1;
    for ($x=0; $x<$foundnum; $x=$x+$per_page)
    {
    if ($start!=$x)
    echo " $i “;
    else
    echo ” $i “;

    $i++;

    }

    if (!($start>=$foundnum-$per_page))
    echo “Next“;

    echo “”;
    }

    }

    ?>

      1. please write here the exact error you got on your browser, copy the error and paste it here. thanks for your comment.

        1. since writing the post i have fixed the error.. the $x and
          $construct… needed defining…. thank you for your explaination. its good to know a fellow bangladeshi with so much passion in php..

          I would like to suggets an adjacent pagination on this tutorial, for eg: page 1, 2, 3, 4, 5……..11, 12, 13, next>>..

          once again thank you for your upload.

          1. I got same error, and I’m still trying to fix it. How did u define the $x and $construct? I thought they had already been defined!

  7. Thank you for the nice tutorial. Im a beginner. It works fine. But i have a peculiar case.
    1. my search form has 3 input search textboxes.
    2. my submit button has an onclick javascript function.

    Please kindly construct the line below :(to add more search fields, and a javascript call.

    echo ” $i “;

    Thank you!

  8. Hi, Thanks for your quick response,
    How can i do it so it limits the search results? for example:
    Prev 1 2 3 4 5 .. 10 11 Next
    My code is in my previous post.

    thank you.

  9. salam i am back,
    is there any way in which files on my wesite will be inserted in the search engine automatically without going to mysql and iserting it manually
    barka allahu feek

    1. @ abdulmalik84 be a bit specific, from what i gathered from your post.. you can by using some thing like this…

      $sql = mysql_query(“INSERT INTO products (product_name, price, details, date_added)
      VALUES(‘$product_name’,’$price’,’$details’,’ now())”) or die (mysql_error());

  10. Hee its a great video, but i have a question about the database. Is there a possibility to set all they internet addresses in to the database, because its impossible to input every adress one by one.. ??

    1. you can use this search engine for your website if your website is database based. you need to change the source code a little just to match with your database table. Actually that’s the main purpose of this search engine. Just like the search engine we have in our website at the top beside the Find source code logo.

      1. I just read the whole page and i see tha more people have the same problem with the pagination ( prev 1 2 3 4 5 6 next ) why you dont answer to that !

        If you dont know how to do that just post it so that people see that.

  11. Can somebody help with the pagination ? and to show it prev 1 2 3 4 5 6 next instead of all numbers from database ?

    Thanks in advance

    1. I have modified the code. have a look from line 61 to 154. I hope now you will have limited pagination. Thank you for your comment.

  12. Great tutorial the search engine and pagination works perfect, but i have the same problem as Mark Vinder. Please HELP !

    1. I have modified the code. have a look from line 61 to 154. I hope now you will have limited pagination. Thank you for your comment.

  13. Thanks for making the pagination 1 2 3 4 etc. but something is wrong in the code.

    Prev 1 2 .. 5 6 7 8 9 10 11 .. 44.0833333333 Next

    Why it shows 44.0833333333 ? and when you click it shows some errors ! >>> how ?

    44.0833333333 must be echo the last page i think ! ?

    1. It’s not suppose to show 44.083333333…. I will check the code again. yes, that’s the last page link. Another option is you can delete the last page link code from the source code as it’s not that important (I think)…. anyway I will check it again and modify the code if it is necessary.

      1. I thin the problem is ( $per_page = 1; ) when i change it to $per_page = 36;

        than it shows the problem and pagination works not so wel..

  14. How is that possible when… $per_page = 36; dont work correct and if i have $per_page = 1; works everything verry good…. ?????

  15. @ MD Mosleh Uddin…….. Change this…….. $max_pages = $foundnum / $per_page;
    to $max_pages = ceil($foundnum / $per_page);….

    Thanks for this great tutorial. with thank to you is my website ready to go online.. keep going with tutorials like this…

    Thanks.

      1. A new problem is showing up Prev 1 2 3 4 5 6 .. 200 Next

        When you change it from $per_page = 1; to $per_page = 36; the result keeps the same..

        The pagination change the content slide + 1 result and not + 36 can you help me with that ?

        Or tell me what i need to change ?

        1. I changed the whole pagination code once again. please modify your code from line no 61 to 154. I hope this time you will get it error free. Let me know if your problem solved. thanks for your continuos comment. Recently I am very busy, that’s the only reason behind my late response.

  16. Why you just dont make a inlog account for people to log in , and add some members. ? and make your website bigger and famous for web developpers. ?

    1. I am very happy to hear that the problem is solved…. :-) ….. actually we have thought about it & soon we will open register/log in option for developers to contribute in our website. Please keep in touch with us. :-)

  17. Great job! it is really easy to follow. Thanks for make wonderful world. my is working perfectly.
    Hopefully you will create a search engine with showing image in the results.
    I would like to have that search engine. Thanks again.

    1. echo ”;

      Like that you can show the image..

      And if you want to make the image linkable than..

      echo ‘
      ‘;

      This will work.

  18. Hi I am a beginner … please I want you to document exposure (explanation) of the program in some detail
    With sincere thanks in advance

  19. @Mosleh i have a question , how can i show
    page=1 instead of
    Search+source+code&start=36

    i dont have any idea how to complete that !

    Thanks in advance.

  20. Hi there

    Many thanks for the pagination php script. Would it be possible for you to amend this script so that you can have a sponsored results appear in a section above the main search results, similar to google search engine.

    Say that I have customers that want to appear above all the rest because they have paid extra for their listing.

    What kind of php would be needed and what would need to be done the database?

    any help would be great.

    Many thanks

    1. Do you mean that u want to echo out your customers above the search engine ?
      is so, than u need to put the names of your customers in to your database, and wehen you do that you can echo out above the search engine, how much customers do you have and want to put them above.?

  21. why when in / search.php emerged this paper?

    Parse error: syntax error, unexpected T_VARIABLE in / home/u347114204/public_html/search.php on line 3

      1. sir help me
        this is my error

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

        Notice: Undefined index: start in C:\xampp\htdocs\search.php on line 42

  22. @Mosleh. Could you change $per_page = 1; if i change it to $per_page = 36; it shows me in the browser search.php?search=$search&submit=Search+source+code&start=36
    search=$search&submit=Search+source+code&start=72
    search=$search&submit=Search+source+code&start=108
    instead of
    search=$search&submit=Search+source+code&start=2
    search=$search&submit=Search+source+code&start=3
    search=$search&submit=Search+source+code&start=4

    Is that possible ?

    Thanks in advance

    1. thanks for your comment. actually to do that I need to change a lot in the pagination script as I wrote the script using the number of rows I got from a single search. Yeah, obviously it’s possible. But currently I am little busy. So If I have some free time I will try that & inform you. A big thank you for visiting our website regularly. Please keep in touch with us.

  23. I am ready to launch the website`s but it is not nice to show it like that in the browser but ok.

    Is it possible to make from this
    echo ” 1 “;

    php server self ? so that i can include the file and when you change the script that i cahnge the script also ?

    I wil keep in touch, like i sead before. Thanks for your response

  24. i m getting a Warning: “mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\view\search.php on line 28
    Sorry, there are no matching result for me

  25. Hello! I would like to ask how to add “minimap” in my game? I want to make a game but do not know how to add “minimap” .. varog if you can help me?

  26. Hi MD Mosleh,

    if i click the page 2 on the script it shows this “Search term too short” i am searching 3 characters only.

  27. salam bhai jaan. im using your code

    i got error in Undefined index: start in C:\wamp\www\engine\search.php on line 43

    plz help.

  28. Dear Mosleh,

    I would like to discuss consulting position with you, in return I can offer share in my company. My guys need direction on how to accomplish PHP tasks, they are accomplished but get stuck, your presence could jettison our project.
    Even 1-2hrs of weekly or monthly guidance should be good enough.

  29. ( ! ) Parse error: syntax error, unexpected ‘$button’ (T_VARIABLE) in C:\wamp\www\fsc\search.php on line 3

  30. Hi ,

    Is very useful your script but i need to search in multiple tables and please tell me if is posible with this script. I have 10 tables with same variables: id, name, categories, date, keywords. Can you help me ? THx in advance

  31. Got this up and running on my site on the search page.. While trying to rewrite the urls to something more clean I realised that the page numbers are output in multiples of however many items per page you choose.. like if I have 12 per page it’s 0,12,24,36 etc.. This doesn’t work well for what I want which is page 1 ,page 2, page 3 etc… Has anybody modified this to work on page numbers?

  32. This code has been very useful…
    can you please assist me with codes on how to generate report with php and mysql.
    Thanks a lot.

  33. hi….

    i have watched ur tutorial
    How to create a Search engine in PHP with MySQL..it is working…

    but coming to
    PHP & MySQL Tutorial | Create a search engine with pagination…..it is not working
    espicially the pagination is not working
    and also a notice in that is
    Notice: Undefined index: start in C:\xampp\htdocs\pagination\search.php on line 44..

    i have done same that what u have done…
    but the pagination is not working..
    can u help me to solve this problem

  34. hey hi….just now i checked again ur code…

    every thing is working good but,there is a notice in ur code
    Notice: Undefined index: start in C:\xampp\htdocs\pagination\search.php on line 44

    line 44 in my code is
    $start = $_GET[‘start’]; (here it is showing the error)

    i have done same that what u have done in ur tutorial

    can u solve this problem???

  35. I have Error Come

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

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

  36. Great and clear lesson for a beginner like me.Thanks so much. Please i have one more request, i want a situation where i can search from more than one column in a table. I have tried doing something like this
    foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1)
    $construct .=”surname LIKE ‘%$search_each%'” OR “othernames LIKE ‘%$search_each%'”;
    else
    $construct .=”AND surname LIKE ‘%$search_each%'” OR “AND othernames LIKE ‘%$search_each%'”;

    }.
    That is using “OR” but is not giving me the result. please how can i achieve that.

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

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

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

    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

  38. Hi & thanks for the code!

    But I also get this error:
    “Notice: Undefined variable: x in C:\xampp\htdocs\_musikarkivet\html\php\for_tutorial\search.php on line 17

    Notice: Undefined variable: construct in C:\xampp\htdocs\_musikarkivet\html\php\for_tutorial\search.php on line 19”

    And I haven’t changed the code more than the connection to my own database. I also have to say that I’m a newbie on PHP coding, but any help or tips would be appreciated.

  39. I am new to php and search engine coding can u please tell me if I don’t wish my search engine to connect to internet instead want it to search items from the server’s library. the files that I am searching is either pdf or excel or both. the search result should also display the searched files data without actually opening the complete file….for example if I am searching for a part name file and its dimension D1 the result should not shows the file instead it should show all the dimensions D1 from all the files present in library for the particular part number in tabular format

  40. I have added css class page to the links and I have a class active how would I assign class active to the current page result?

  41. great engine script but i had some problems with undefined variables.

    1 at line 17
    complete code:
    foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1)
    line 17 error : $x++; … above foreach should of been $x=0;.

    second error:
    $construct .=”” this is not a defined variable it’s just addon … so i deleted the .

    next:

    from line 57 to 62 there was the undefined $URL variable … i deleted it.

    and last, on line 41: $start = $_GET[‘start’];

    this one is from pagination and i don’t know how to adjust it. please help.

    and also, i’d like to know if you can help me put 3 dropdown options on search engine like

    1st dropdown: citizen, company, etc.
    2nd dropdown: city names like Constanta, bucharest, timisoara, etc

    and 3rd dropdown: city neighborhoods like: inel2, tomis nord, etc.

    i know how to make the dropdown options but, i don’t know how to include them in the search terms.

    like : if selected: citizen, Constanta, inel2, should display results from his area (city, neighboorhood) wich will be found in database.

    i’ll wait for your reply as soon as you can. thank you.

  42. how do you add this 1 to 2 of $foundnum(total rows returned). i mean every time a user clicks on the next it should increment the page number. i have tried out the complete script it works for me but i need you to help me with that.

  43. omg i got some erron in
    Notice: Undefined variable: x in C:\xampp\htdocs\search.php on line 17

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

    Notice: Undefined index: start in C:\xampp\htdocs\search.php on line 41

    the code that i copied is the same why is this happen?

  44. Hey i need ur help I want a custom search on my php page means search page having radio buttons, text boxes, check boxes and combo box. If I write the query by using conditional statements(if/else) it will become really mess.. Is there any easy way to do custom search in php…Please help me

  45. This is a really a nice tutorial. But ai facing some problem everything work fine but when i click on search button browser show something error like this

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

    ( ! ) Notice: Undefined variable: construct in C:\wamp\www\search\search.php on line 19
    Call Stack
    # Time Memory Function Location
    1 0.0009 405616 {main}( ) ..\search.php:0
    4 results found !

    ( ! ) Notice: Undefined index: start in C:\wamp\www\search\search.php on line 41
    Call Stack
    # Time Memory Function Location
    1 0.0009 405616 {main}( ) ..\search.php:0

    ( ! ) Notice: Undefined index: url in C:\wamp\www\search\search.php on line 51

    plz help me.

  46. first thank you for you time to show how to do. I have modified and working perfectly the only issue I have the pagenation only display one line even tough the max is 30 per page. where I should look to troubleshoot.

  47. everytime i put this code in i get

    Notice: Undefined variable: x in C:\xampp\htdocs\search2.php on line 18

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

    Notice: Undefined index: start in C:\xampp\htdocs\search2.php on line 42
    find source code
    what am i doing wrong

  48. i am having following error kindly guide to rectify the error

    Notice: Undefined variable: x in D:\wamp\wamp\www\yourneeds\search.php on line 25
    Call Stack
    # Time Memory Function Location
    1 0.0015 275424 {main}( ) ..\search.php:0

    ( ! ) Notice: Undefined variable: construct in D:\wamp\wamp\www\yourneeds\search.php on line 27
    Call Stack
    # Time Memory Function Location
    1 0.0015 275424 {main}( ) ..\search.php:0

    25line $x++;
    26 line if($x==1)
    27 line $construct .=”keywords LIKE ‘%$search%”;

  49. Hello , could some one to help me please.I have these errors: Notice: Undefined variable: x in C:\wamp\www\search.php on line 17

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

    Notice: Undefined index: start in C:\wamp\www\search.php on line 40
    Find source code

    Here’s my code:
    <?php

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

    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”root”,””);
    mysql_select_db(“searchparts”);

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

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

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

    $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 !”;

    $per_page = 1;
    $start = $_GET[‘start’];
    $max_pages = ceil($foundnum / $per_page);
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM searchengine WHERE $construct LIMIT $start, $per_page”);

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

    echo ”
    $title
    $desc
    $url
    “;

    }

    //Pagination Starts
    echo “”;

    $prev = $start – $per_page;
    $next = $start + $per_page;

    $adjacents = 3;
    $last = $max_pages – 1;

    if($max_pages > 1)
    {
    //previous button
    if (!($start<=0))
    echo " Prev “;

    //pages
    if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    {
    $i = 0;
    for ($counter = 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
    {
    //close to beginning; only hide later pages
    if(($start/$per_page) < 1 + ($adjacents * 2))
    {
    $i = 0;
    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //in middle; hide some front and some back
    elseif($max_pages – ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //close to end; only hide early pages
    else
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    }

    //next button
    if (!($start >=$foundnum-$per_page))
    echo ” Next “;
    }
    echo “”;
    }
    }
    ?>

  50. Brother through your past commentaries and replies i can tell u are very responsible pls i need u to help me with my dream of creating a website i can make money with brother pls i need tutorials on how to create such website.

  51. everything explain except for the variable $x, $construct and $start, pls. can any one explain how to deal with this variables are..thanks.. especially the $start=$_GET[‘start’]; your form doesn’t have a string $_GET[‘start’]!!! what should be in their to populate the variable $start..

  52. Help please

    Notice: Undefined variable: x in C:\wamp\www\buscadordos\search.php on line 17

    Notice: Undefined variable: construct in C:\wamp\www\buscadordos\search.php on line 19

  53. Notice: Undefined variable: x in C:\Program Files\EasyPHP-5.3.9\www\Mobile Info Arena\searchengine.php on line 17

    Notice: Undefined variable: construct in C:\Program Files\EasyPHP-5.3.9\www\Mobile Info Arena\searchengine.php on line 19
    Sorry, there are no matching result for nokia.

    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

    i have a error in 17 and 19 line plzz help me

  54. Notice: Undefined index: submit in C:\Program Files\EasyPHP-5.3.9\site\searchengine.php on line 3

    Notice: Undefined index: search in C:\Program Files\EasyPHP-5.3.9\www\site\searchengine.php on line 4
    Search term too short

  55. Thank you!!!! Finally someone who can properly explain the coding and provide an error free tutorial! I’m now subscribed on your youtube channel.

  56. SIR

    how to parse XML affiliate code with database ..where i have to parse my xml code

    please help me any one for example my affiliate code like this http://good……..xml

    where i have to set and i want show xml ads via search …any one help

    thnks

  57. Hey admin…All is working awesome but there is only one problem when I click on search button to search something..the problem is… Notice: Undefined index: start …It is only on the first page and on the other pages its not appearing only page 1 fucking me up…please help..waiting for your reply…

  58. searchNotice: Undefined variable: x in C:\Users\user\Desktop\xampp\htdocs\google\search.php on line 17

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

    Notice: Undefined index: start in C:\Users\user\Desktop\xampp\htdocs\google\search.php on line 41

  59. your tutorial helps me lot….Thanks for it
    But if i need to search database using more than one keyword..how should i change the code

  60. If i have use $constructs =”SELECT * FROM bridegroom WHERE Castest=’$caste’and Gender=’$gender'”; error is shown what will be the cause for it?

    ERROR:Undefined index: start in C:\wamp\www\matrim\search.php on line 37

  61. Hi this a very nice tutorials for search and pagination but i was wondering if i have about four fields for searching how do i adjust the code to give results thank you for the good work

  62. I got this error please help me
    SCREAM: Error suppression ignored for
    ( ! ) Notice: Undefined index: start in C:\wamp\www\modules\pagenation\search.php on line 41

  63. used same codes till giving this errors…….
    its working but with tis errors……

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

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

    Notice: Undefined index: start in C:\xampp\htdocs\tutorial\search1.php on line 41

  64. foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1){
    $construct =”title LIKE ‘%$search_each%'”;
    }else{
    $construct =”AND title LIKE ‘%$search_each%'”;
    }
    } here getting error for this x veriable as undefined one

  65. its searching

    <?php

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

    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”root”,””);
    mysql_select_db(“for_tutorial”);

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

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

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

    $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 !”;

    $per_page = 10;
    $start = $_GET[‘start’];
    $max_pages = ceil($foundnum / $per_page);
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM searchengine WHERE $construct LIMIT $start, $per_page”);

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

    echo ”
    $title
    $desc
    $url
    “;

    }

    //Pagination Starts
    echo “”;

    $prev = $start – $per_page;
    $next = $start + $per_page;

    $adjacents = 3;
    $last = $max_pages – 1;

    if($max_pages > 1)
    {
    //previous button
    if (!($start<=0))
    echo " Prev “;

    //pages
    if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    {
    $i = 0;
    for ($counter = 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
    {
    //close to beginning; only hide later pages
    if(($start/$per_page) < 1 + ($adjacents * 2))
    {
    $i = 0;
    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //in middle; hide some front and some back
    elseif($max_pages – ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //close to end; only hide early pages
    else
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    }

    //next button
    if (!($start >=$foundnum-$per_page))
    echo ” Next “;
    }
    echo “”;
    }
    }
    ?>
    ……………….
    here is full codes

  66. here is errors….
    Notice: Undefined variable: x in C:\xampp\htdocs\tutorial\search1.php on line 32
    102 results found !

    Notice: Undefined index: start in C:\xampp\htdocs\tutorial\search1.php on line 56…
    ………. plz help

  67. Hello MD Mosleh Uddin.
    I do it on wamp server its shows me two Error that is …
    1st one is
    (Notice: Undefined variable: x in E:\wamp\www\sample\new_serachengine\search.php on line 25)

    and 2nd
    (Notice: Undefined variable: construct in E:\wamp\www\sample\new_serachengine\search.php on line 27)

    and 3rd is
    ( Notice: Undefined index: start in E:\wamp\www\sample\new_serachengine\search.php on line 49)

    help me to solve it out

  68. Hi,
    Your code is good. I found some search.php from other website. But i like to use your one. Its working in Internet Explore, Safari. But i can’t make it work in Firefox & Chrome. They show only “Search term too short”, how long i searching for.

    It match only from title, not from description.

    Another thing is-
    Is it possible to make search result within 5 lines.

    Thank you.
    Some One.

  69. plz help me

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

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

  70. Here is my again sir! (1) use random number with aplhabet e.g 34555666777DF in php and MySQL (2) use of pin and serial number for registration 1 time and to use that the same pin and serial number to reprint submitted information through login 4 times and it will not enter again in php and MySQL. Pls, I will waiting for the reply. Thanks sir in advanced may Allah bless you!

  71. Sir,

    when i run the above code following error comes:-

    1st one is
    (Notice: Undefined variable: x in E:\wamp\www\sample\new_serachengine\search.php on line 25)

    and 2nd
    (Notice: Undefined variable: construct in E:\wamp\www\sample\new_serachengine\search.php on line 27)

    and 3rd is
    ( Notice: Undefined index: start in E:\wamp\www\sample\new_serachengine\search.php on line 49)

    help me to solve it out
    and reply me asap

  72. Hi

    I had same problems for the $x,$constructor and $ start.

    $x => declare $x=0; above the foreach loop and same for $constructor =””;

    but for start it is little bit different :
    $per_page = 2; // set to 2 instead of 1

    add this if condition to check if it null or undefined

    if(isset($_GET[‘start’]))
    {
    $start = $_GET[‘start’];
    }else{
    $start =0;
    }

    it works for me, good luck

  73. Respected sir.. it work good.. thank for the wonderful program..

    but i hav a problem in the above prg.. when i run it it shows

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\search\search.php on line 28
    Sorry, there are no matching result for how to create a web site.

    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..

    how to rectify it..??
    plz help me its urgent

  74. Sir, finally i make two file one search.php &second one is index.php in new folder and when i add query in searchbox in feedback the full search.php script is shown
    i open file in opera browser

  75. ehen i enter keyword in box it says

    Parse error: syntax error, unexpected ‘$button’ (T_VARIABLE) in C:\xampp\htdocs\pagination\search.php on line 4

  76. Hi,
    I try to build a multiple search columns, I have successfuly got a search result from two database columns (model and color) by changing the source code to:

    foreach($search_exploded as $search_each)
    {
    $x++;
    if($x==1)
    $construct .=”model LIKE ‘%$search_each%’ OR color LIKE ‘%$search_each%'”;
    }

    for exemple I put “Nike” in model column and “Red” in color column, the search.php page display me all results for “Nike” and all results for “Red”.
    But I want to get only result that match the “model” and the “color” mean I want a combined result only for a “red Nike”, I have tried to do with “AND” instead of “OR” but it show me any results.
    Please can you show me how can I get a combined result?

  77. i am struck between this bro can any 1 of u help me solving my problem…..

    ( ! ) Notice: Undefined variable: x in C:\wamp\www\dock\searchengine.php on line 18
    Call Stack
    # Time Memory Function Location
    1 0.0010 279328 {main}( ) ..\searchengine.php:0

    ( ! ) Notice: Undefined variable: construct in C:\wamp\www\dock\searchengine.php on line 20
    Call Stack
    # Time Memory Function Location
    1 0.0010 279328 {main}( ) ..\searchengine.php:0
    7 results found !

    ( ! ) Notice: Undefined index: start in C:\wamp\www\dock\searchengine.php on line 42
    Call Stack
    # Time Memory Function Location
    1 0.0010 279328 {main}( ) ..\searchengine.php:0

  78. Thank you so much for the video and code, I have been looking for something like this for months. I am new to php and mysql, you did a great job of making this simple!

    What code do I use to get user back to the index page after the search? Also How would I do a drop down box aswell or intsead of the search bar. For the drop down, I want to use the SELECT DISTINCT name from tbl1. How would I do that?

  79. Well done, thank you for sharing. where can I download the source code for the search with pagination in zip format?

  80. Sorry I don’t why this error keep showing
    Notice: Undefined variable: x in C:\xampp\htdocs\project\book\search.php on line 17

    Notice: Undefined variable: construct in C:\xampp\htdocs\project\book\search.php on line 19

  81. any one help me to create the search Engine for multiple tables there since am trying it cant work and how to link the emails of clients to i dont have any idea

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

    Notice: Undefined variable: construct in C:\xampp\htdocs\xss\search.php on line 19
    this error i cant solve
    i follow all step but i got this error

  83. Hello there, I am doing a project for class and I ran across your search engine tutorial and I do absolutely love it. I was wondering why the search results is not showing. The code is working but for some reason it is not appearing in the search results. Any suggestions may help? I have put my database information, the table that you set up in the video, everything. Thank you Have a wonderful day!

  84. brother, can you help me with this error, i got an error as i search something on the search bar

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

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

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

    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

  85. Hi, the code is perfect and working fine but I recommend you to provide the sql files also in this tutorial specially for the beginners in php.

  86. <?php

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

    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;
    mysql_connect(“localhost”,”root”,”29091992″);
    mysql_select_db(“search”);

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

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

    }

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

    $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 !”;

    $per_page = 1;
    $start = $_GET[‘start’];
    $max_pages = ceil($foundnum / $per_page);
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM searchengine WHERE $construct LIMIT $start, $per_page”);

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

    echo ”
    $title
    $desc
    $url
    “;

    }

    //Pagination Starts
    echo “”;

    $prev = $start – $per_page;
    $next = $start + $per_page;

    $adjacents = 3;
    $last = $max_pages – 1;

    if($max_pages > 1)
    {
    //previous button
    if (!($start<=0))
    echo " Prev “;

    //pages
    if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    {
    $i = 0;
    for ($counter = 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
    {
    //close to beginning; only hide later pages
    if(($start/$per_page) < 1 + ($adjacents * 2))
    {
    $i = 0;
    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //in middle; hide some front and some back
    elseif($max_pages – ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //close to end; only hide early pages
    else
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    }

    //next button
    if (!($start >=$foundnum-$per_page))
    echo ” Next “;
    }
    echo “”;
    }
    }
    ?>
    Used the Same code but But getting errors like below shown

    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: start in C:\xampp\htdocs\search\search.php on line 41
    Manam Movie
    Manam Movie
    Manam Movie
    I am Getting the results of searchengine but what is the undefined variable which is showing in script
    Please Fix the Code

  87. $constructs =”SELECT * FROM `searchengine` WHERE $construct”;
    $run = mysql_query($constructs);

    $foundnum = mysql_num_rows($run);
    problem continue

  88. Dear MD Mosleh Uddin,

    This is more than just a tutorial. It’s a solution. May God bless and continue to increase your knowledge.

  89. nice tutorials but question was it necessary to use $getquery when it was already executed and stored in $run because in essence it would have global scope as it wasn’t use in a function already

  90. Mosleh you are a great teacher. It would be great if you or your readers can add some more search algorithms to play around with or some more sophisticated regular expressions in the WHERE clause. What do you think?

  91. Hello,sir ..
    Above code are working,but ,may be possible that with image search. That mean we are add image option in database.So
    First how to insert image in database.
    Second thing that,after insert image then how to retrieve image with all field(url,description,title etc.).Plz sir may possible that make searching code with image in database..

  92. And I m request how to make email without mail server ?
    Plz i m want to make site and i want send email verification.so how to possible ?
    I m waiting your reply …

  93. Hello Mosleh im getting error with your code.

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\LEGISLATIVE\admin\samplesearch.php on line 31

    and i already change the database table name in line 31

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

    $foundnum = mysql_num_rows($run);

    Please help.

  94. Hi, thank you for this awesome tutorial, I’m a beginner in PHP programming and this helped me a lot

    If you have ever tried cakePHP framework, please do post tutorials as well. Thanks again.

    Sunil. modify line 37.
    $per_page = 1;
    change it to $per_page = 5;

  95. Awesome tutorial :)

    BTW if anybody knows how to be able to search multiple columns let me know :)
    For example I want to be able to search for strings in column 1 + column 2 like “Creak Cookanne is bad” where “Creak” is in column 1 and “Cookanne is bad” in column 2.

    I tried several ways, but in one instance I could only input 1 word from each column, the next instance the relevancy was 0. I was tweaking with the if $construct and else $construct, since I’m a beginner I don’t know what else can be done.

    Anyways thanks in advance 😀

  96. Hi,
    I have a question about your code. How can I use CSS style in the php echo? I tried to put something inside the echo, when it searched I just got blank page. How could I fix this? Thank you.

  97. Help ME

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

  98. Hello James,
    Problem Solved —->
    Notice: Undefined variable: x in C:\xampp\htdocs\search.php on line 17
    1 results found !

    Notice: Undefined index: start in C:\xampp\htdocs\search.php on line 42

    please review below code
    i created another file for database connectivity (connect.php)
    ——————————————————————–

    Title of your search engine

    My Search Engine

    <?php
    if(isset($_GET['submit'])){
    $button = $_GET['submit'];
    $search = $_GET['search'];

    if(strlen($search)<=1)
    echo "Search term too short";
    else{
    echo "You searched for $search “;

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

    $x = “”;
    $construct = “”;

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

    }

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

    $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 !”;

    $per_page = 1;
    $start = isset($_GET[‘start’]) ? $_GET[‘start’]: ”;
    $max_pages = ceil($foundnum / $per_page);
    if(!$start)
    $start=0;
    $getquery = mysql_query(“SELECT * FROM search_job WHERE $construct LIMIT $start, $per_page”);

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

    echo ”
    $title
    $desc
    $url
    “;

    }

    //Pagination Starts
    echo “”;

    $prev = $start – $per_page;
    $next = $start + $per_page;

    $adjacents = 3;
    $last = $max_pages – 1;

    if($max_pages > 1)
    {
    //previous button
    if (!($start<=0))
    echo " Prev “;

    //pages
    if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    {
    $i = 0;
    for ($counter = 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
    {
    //close to beginning; only hide later pages
    if(($start/$per_page) < 1 + ($adjacents * 2))
    {
    $i = 0;
    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //in middle; hide some front and some back
    elseif($max_pages – ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }

    }
    //close to end; only hide early pages
    else
    {
    echo ” 1 “;
    echo ” 2 …. “;

    $i = $start;
    for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
    {
    if ($i == $start){
    echo " $counter “;
    }
    else {
    echo ” $counter “;
    }
    $i = $i + $per_page;
    }
    }
    }

    //next button
    if (!($start >=$foundnum-$per_page))
    echo ” Next “;
    }
    echo “”;
    }
    }

    }
    ?>

  99. I got this web page from my pal who shared with
    me regarding this web page and at the moment this time I
    am visiting this web page and reading very informative articles at this place.

  100. hello sir, thx for this tutor,
    i want ask something
    can u teach me how, if i want search code like number?
    please help me
    thx u sir

  101. Hi MD Mosleh, there is a good tutorial and it work perfectly.If do have 2 or 3 input field to match these input query to search the result,how to go about it in your source code ? Kindly advise…Thanks in advance.

  102. Hi MD Mosleh, there is a good tutorial and it work perfectly.If do have 2 or 3 input field to match these input query to search the result,how to go about it in your source code ? Kindly advise…Thanks in advance.

  103. Hello
    thanks for this tutorial
    I have done every thing and collation utf-8
    but in search, persian charachters not work and when search english words I see that persian charachters have changed to ???? ??? ??? .
    I set up php for working with Unicode data and is correct but responing from the database is ???? ????
    I do not know what to do

  104. sir , when i hit search button after entering keyword im getting php code for search.php file !! plz solve my query as soon as possible ! also friends ur suggestion are also welcomed

  105. thank you for the tutorial it really helpful, but am having little issue with the search engine , whenever i try to search it will come up with a message “Search term too short”

  106. Thanks so much for this tutorial; i just need an extra help, What if i want to get search results from two tables; is that possible? how do i do it? Thanks in advance.

  107. i have a problem on line 32 please help me this is for my thesis

    “You searched for facebook

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search\search.php on line 32
    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”

  108. Hi MD MOSLEH UDDIN sir,
    I’m just a beginner web builder with limited knowledge about PDO database connection. The code that you provided in this page works completely fine without any errors. I was trying to test this code using PDO for couple of days but I wasn’t able to solve it. Could you please help me with this issue by coding the same code using pdo connection. It would be much much appreciation.
    Thank you

Leave a Reply

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

four − 3 =