Publishing System Settings Logout Login Register
Create Your Own PHP MYSQL Search Engine
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on November 23rd, 2007
57980 views
PHP Coding
[MY_P2L_AD]
Hey Guys,

This is my second php tutorial written in P2L.com. Ok so this time the tutorial is about How to make your Own Search Engine.

Now, I should prelude this by saying that there are about two trillion ways of designing and implementing a search engine, but this is a quick easy one (in fact, you can basically copy/paste the source code if you don't feel like reading the tutorial).


 Okay, So this our fist Code with what we should begin just after  <?php ....


$var = @$_GET['q'] ; // get the query for the search engine (if applicable)
$trimmed = trim($var); //trim whitespace from the stored variable



This PHP code snippet should go in above the <head> of the document. The first line retrieves the "q" ("q" stands for query, in case you were wondering) from the search. The second line strips any white spaces.

Next we're going to need to connect to the database...


$user = "username"; // AN EDIT IS REQUIRED HERE
$password = "password";
$host = "host";
$dbase = "dbase";
$table = "table";

// Connection to DBase
mysql_connect($host,$user,$pass);
@mysql_select_db($dbase) or die("Unable to select database");



The above variables need to be set (obviously) to whatever is applicable to your host/database.

Okay, now we're going to talk about the database structure a little bit. Basically, what I've done is added a field to the dbase called "keywords". Then, I entered comma-separated values that I wanted to associate with that particular letter. Take a look at the code:

$field_to_search = "field";
$query = "SELECT * FROM $table WHERE $field_to_search LIKE "%$trimmed%" order by id";

$result = mysql_query($query);
$count =mysql_numrows($result);


So, these four lines do this:


1) Set the field to search as a variable called $field_to_search
2) Query the database for the searched term
3) Return the results
4) Count the results (for aesthetic purposes later)


Up to this point, all of this PHP code should be in or above the <head> section. To be 100% honest, it doesn't really matter if it is or isn't in the <head> section, but I like to keep my "backend" PHP up there because it's "out of the way" (so to speak).

This first code snippet is NOT PHP. It shouldn't be put in the <? ?> tags.


<form name="search" method="GET" action="<?=$PHP_SELF?>">
Seach the database for: <input type="text" name="q" />
<input type="submit" name="search" value="Search" />
</form>



The above code snippet is the HTML form for querying the database. If you want, you can set up a snippet of code to hide the form when someone has already done a search, because the results will be displayed on the same page. You could do that with this code:


if($q == true)
{
exit;
}
elseif($q == false)
{
<form name="search" method="GET" action="<?=$PHP_SELF?>">
Seach all the newsletters for
<input type="text" name="q" />
<input type="submit" name="search" value="Search" />
</form>
}



Okay, that's just a side note... on to displaying the results!

This is a lot of code, but it's pretty self-explanatory... Just follow the comments in the code, and you should have a good idea what's going on.


if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s))
{
$s=0;
}

// get results
$result = mysql_query($query) or die("Couldn't execute query");

if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

// display what the person searched for
echo "<p>Your search for "" . $var . "&quot returned $numrows $return.</p>";

// begin to show results set
$count = 1 + $s ;



Okay, I've chosen to interrupt the code here to explain the following while() loop. If you've worked with PHP/mySQL for any length of time at all, you've probably set up a loop just like this one, but I'll assume that you haven't.

What you're going to want, so to set a variable, then put an = sign, then an $r[""];. in the quotes ("), you're going to add the name of the field that you want to associate with that variable. I'll show you some examples, but please now that you're going to need to change this to fit your database.


while ($r= mysql_fetch_array($result))
{
$id = $r["id"];
$year = $r["year"];
$date = $r["date"];
$title = $r["title"];
$description = $r["description"];

$count++ ;
?>



So now you have your while() loop set up. The last line there is $count++;. Normally, you would end a while loop with the closing bracket (}), but we are going to "break out" of the while() loop here to format our results. By breaking out of the while loop to enter some good ol' HTML, you'll result the HTML every time the while loop passes, so, in other words, for every result of your search! Pretty clever, huh?

Using the above example database, I'll show you what you could do... Keep in mind, this needs to be OUTSIDE of the <? ?> tags... rather, between them.


<a href="http://www.YOURDOMAIN.com/archive/<? echo $year ?>/<? echo $id ?>.html"><? echo $title ?></a><br /><br />

<? echo $description ?><br />
Result Number: <? echo $count ?>



Okay, so if your search returned 12 results, all your html would be written out for you with the link, the title, the description, and letting you know which result number you're seeing!

Now, a very important piece of code:


<? } ?>



You have to close your while loop!!!! Many many frustrating hours I have spent debugging source code because I forgot those eight little characters!



Ok so see next time in my furthur tutorials
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
AFG89

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top