|
| Web Hosting Deals | Holiday Logo Design | Webcam Chat | Website Header Templates | Register domain | Search Engine Optimisation | Web Hosting |
|
|||||||
| PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials. |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Creating A Simple Search Engine In PHP (ver. 1.0.0)
Introduction:
This is the first in what I hope will be a long list of tutorials that I like to call Practical Programming in PHP. Lets get down to business, today I'm going to show you how to create a simple search engine that you can use for your very own site or some other project that you may be working on. For this tutorial I will be using a MySQL database. The bulk of the code should be compatible with PHP 3 and 4. Background: Before we begin, I'll show you the table I'll be using in the examples: First_Name Middle_Name Last_Name Dana Johnson Smith Jill Angel Petersburg Jack Coner Mitchel If you feel confused about any of the functions in the tutorial, have a look at my previous tutorial that deals with common database functions. Down To Work: Before we actually make the search engine, we need to create a basic webpage that will have an input field where the user can enter his or her search query. I'm going to keep mine simple; feel free to make an elaborate one with lots of bells and whistles. The code for my page is below: <html> <head> <title>Simple Search Engine version 1.0</title> </head> <body> <center> Enter the first, last, or middle name of the person you are looking for: <form action="search.php" method="post"> <input type="text" name="search_query" maxlength="25" size="15"><br> <input type="reset" name="reset" value="Reset"> <input type="text" name="submit" value="Submit"> </form> </center> </body> </html> It's a pretty basic page so I'm not going to explain alot of it. Basically, the user will enter the first, middle, or last name of the person they are looking for and hit enter. The contents of the input field will be passed to a php script named “search.php” which will handle the rest. Now that the page is out of the way, let's create the actual script. First, we need to connect to the database using mysql_pconnect() and select the table using mysql_select_db(). Next, we want to parse the value passed to the script to see if it contains any invalid input, such as numbers and funky characters like #&*^. You should always validate input, don't rely on things like JavaScript to do it for you, because once the user disables JavaScript all that fancy validation goes down the toilet. To check the input we are going to use a regular expression, they are a bit confusing and will be explained in a later tutorial. For now, all you need to know is that it will check to see if value passed is a string of characters. All right, enough chatter, here is the first part of the script: <?php mysql_pconnect("host", "username", "password") or die("Can't connect!"); mysql_select_db("Names") or die("Can't select database!"); if (!eregi("[[:alpha:]]", $search_query)) { echo "Error: you have entered an invalid query, you can only use characters!<br>"; exit; } Now that we've done that, we will form the search query. $query= mysql_query("SELECT * FROM some_table WHERE First_Name= '$search_query' OR Middle_Name= '$search_query' OR Last_Name= '$search_query' ORDER BY Last_Name"); Look confusing? I'll explain, what's happening is, we're asking MySQL to search all the rows in First_Name, Middle_Name, and Last_Name for a match to the query entered by the user; then, take the results of that search, alphabetize the results by Last_Name. The rest of the coding from now on is a breeze. We will get the results from the query using mysql_fetch_array( ), and check to see if there is a match using mysql_num_rows(). If there is a match, or matches, we will output it along with the number of matches found; if there isn't, we'll report to the user that we couldn't find anything. $result= mysql_num_rows($query); if ($result == 0) { echo "Sorry, I couldn't find any user that matches your query ($search_query)"; exit; } else if ($result == 1) { echo "I've found <b>1</b> match!<br>"; } else { echo "I've found <b>$result</b> matches! <br>"; } while ($row= mysql_fetch_array($query)) { $first_name= $row["First_Name"]; $middle_name = $row["Middle_Name"]; $last_name = $row["Last_Name"]; echo "The first name of the user is: $first_name.<br>"; echo "The middle name of the user is: $middle_name.<br>"; echo "The last name of the user is: $last_name.<br>"; } ?> I added that extra if statement so that when we report how many users we've found, its output will be in proper English. If I we don't, the script will echo "I've found 1 matches" which obviously isn't good grammar :P The rest of the script loops through the results and prints them to a webpage. That's all, we've finished the script! The entire script is included below: <html> <head> <title>Simple Search Engine version 1.0 - Results </title> </head> <body> <?php mysql_pconnect("host", "username", "password") or die("Can't connect!"); mysql_select_db("Names") or die("Can't select database!"); if (!eregi("[[:alpha:]]", $search_query)) { echo "Error: you have entered an invalid query, you can only use characters!<br>"; exit; //No need to execute the rest of the script. } $query= mysql_query("SELECT * FROM some_table WHERE First_Name='$search_query' OR Middle_Name= '$search_query' OR Last_Name='$search_query' ORDER BY Last_Name"); $result= mysql_numrows($query); if ($result == 0) { echo "Sorry, I couldn't find any user that matches your query ($search_query)"; exit; //No results found, why bother executing the rest of the script? } else if ($result == 1) { echo "I've found <b>1</b> match!<br>"; } else { echo "I've found <b>$result</b> matches!<br>"; } while ($row= mysql_fetch_array($query)) { $first_name= $row["First_Name"]; $middle_name = $row["Middle_Name"]; $last_name = $row["Last_Name"]; echo "The first name of the user is: $first_name.<br>"; echo "The middle name of the user is: $middle_name.<br>"; echo "The last name of the user is: $last_name. <br>"; } ?> </body> </html> |
|
|||
|
I do not have an idea about this.
Thank you, Regards, Pakistan Boy, The Best Link Directory | 1 Pakistan Gifts |
|
|||
|
This is useful if you know what you are searching for. But if I don`t know what was the name, Mitchel, or Mitchal (sounds weird, but for example
), what should I do? ![]() I can suggest you to change the mysql query to this: PHP Code:
![]() anyway, cheers and good tutorial indeed ![]() Last edited by triXtyle; 06-19-2008 at 01:00 AM. |
|
||||
|
(Azim)
HTML sites are only ment for text. Personal Portfolios and simple company sites use HTML. Places like myspace.com and google.com use php/mysql. But I do believe a php code can work on a html site. If i'm wrong someone please correct me. (TUTORIAL) It looks simple enough, from what I see. I don't know much about PHP/MYSQL but I want to know if it is possible to put it on a side like a google search bar. And make it open up in a new window to a forum search?
__________________
http://img295.imageshack.us/img295/1...erssidemr8.jpg |
|
|||
|
sdavis891, that is very possible
![]() For it opening in a new window, that is html coding, changing the target from blank to parent (i think thats the right ones, i always get them mixed up )I dont understand your question about putting it on a side. Are you talking about the positioning of the search bar? |
|
||||
|
yes. Having a search bar located on the top of a website. When someone enters there information and presses go or enter... It opens a new browser IE/FF3 or what ever... It comes up to a page with there information submitted and showing a list of results.
But what i want to know if its possible to plug it into a forum search.. What I was saying earlier was that if I made one and I clicked go. It would open up my forum search with my text displaied with results. This is going from a Website -> Forum
__________________
http://img295.imageshack.us/img295/1...erssidemr8.jpg |
|
|||
|
Thanks for help guys,
And u r right i was talking about making a search engine in HTML site using php&Sql. it is possible, but i dodnt have tutorials. Thanks And about ur searches maybe u r using the forum search engine which takes you there. Did you direct the queries to the forum's database? This is what could possibly be wrong. Well Thanks again guys. |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|