Completing a Search Engine with MySQL and PHP 5 - Developing a fully-functional practical example (
Page 4 of 4 )
In consonance with the concepts deployed in the previous section, I'm going to set up an example where all the PHP classes defined earlier will be used in conjunction to make this search engine work as expected.
In this case, I'm going to use a simple "USERS" MySQL database table, populated with some basic records to illustrate the functionality of the search engine in question. It's quite possible, however, that in a real situation you'll need to perform the pertinent search queries against multiple tables.
Okay, assuming that the aforementioned "USERS" table is the same one that I used in the first article of the series, it is filled in with the following data:
|
Id |
firstname |
lastname |
email |
comments |
|
1 |
Alejandro |
Gervasio |
alejandro@domain.com |
MySQL is great for building a search engine |
|
2
|
John |
Williams |
john@domain.com |
PHP is a server side scripting language |
|
3 |
Susan |
Norton |
sue@domain.com |
JavaScript is good to manipulate documents |
|
4 |
Julie |
Wilson |
julie@domain.com |
MySQL is the best open source database server |
Here's a basic example that demonstrates how this extensible search application functions:
try{
// include classes
require_once 'sessionhandler.php';
require_once 'mysql.php';
require_once 'webpage.php';
// create new session handler object
$sh=new SessionHandler();
// connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user',
'password'=>'password','database'=>'database'));
// create new web page object
$wp=new WebPage();
// check if search term has been saved to session variable
if(!$sh->getVariable('searchterm')){
$searchterm=$db->escapeString($_GET['searchterm']);
$sh->setVariable($_GET['searchterm'],'searchterm');
}
else{
// get search term from session variable
$searchterm=$sh->getVariable('searchterm');
}
// display header
echo $wp->displayHeader();
$result=$db->query("SELECT firstname, lastname,comments FROM
users WHERE MATCH(firstname,lastname,comments) AGAINST
('$searchterm' IN BOOLEAN MODE)");
if(!$result->countRows()){
echo $wp->displayBody('<div class="maincontainer"><h2>No
results were found. Go back and try a new search.</h2></div>');
}
else{
// display search results
echo $wp->displayBody('<div class="maincontainer"><h2>Your
search criteria returned '.$result->countRows().'
results</h2>'.$result->fetchPagedRows($_GET['page']).'</div>');
}
// display footer
echo $wp->displayFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As shown above, putting the search engine to work is a fairly easy process. It requires only using all the classes that were previously defined, in addition to performing the respective search queries by taking advantage of the full-text and Boolean capabilities offered by MySQL.
Finally, take a look at the following screen shots. They show the different database results returned by the search engine, according to certain search strings entered in the corresponding web form:
(results returned by entering the search string "Alejandro")

(results returned by entering the search string "Alejandro+Susan")

(results returned by entering the search string "Alejandro+Susan+John")

As you can see in the previous images, building a search engine using the powerful MySQL/PHP 5 combination is indeed a no-brainer process that can be tackled with minor hassles. At this point, you have at your disposal all the required source files to incorporate this application into your own web site, and provide users with a simple mechanism to search and find your nicely-crafted contents.
Final thoughts
Unfortunately, this is the end of the series. Nonetheless, I think that the whole experience has been educational, since it illustrated in a friendly fashion how to build an expandable search engine by using the capabilities provided by MySQL and PHP 5.
See you in the next PHP tutorial!
| | Discuss Completing a Search Engine with MySQL and PHP 5 | | | | | | | Over the course of this final tutorial of the series, this MySQL/PHP 5 - based... | | | | | | It's a decent simple search engine, but that's all really. It gets tougher when you... | | | | | | Thank you for commenting on my PHP article. You’re correct regarding the simplicity... | | | | | | Getting a grasp on classes in php is difficult for some, like myself, but your... | | | | | | Thank you for the kind comments on my PHP article, and it's always good to know it's... | | | | | | Hi,
Thanks for the detailed tutorial. I began with confidence but I have... | | | | | | Thank you for the comments on my PHP article. Now, concerning your question, the... | | | | | | Hi Alejandro,
Thank you for taking your time to reply. I read your entire... | | | | | | Hi JSP,Thanks again for posting here. Now that you've created a "processform.php"... | | | | | | Thanks Alejandro,
That helps... WOW I wouldn't have guessed that, since I am no... | | | | | | Hi JSP,
Glad to know the search engine is working for you. Thanks again for the... | | | | | | Hi,
I am enjoying working through some of your tutorials am really pretty new to... | | | | | | Hi Joe,
Thanks for the comments on my PHP article. In fact, that’s not a bug of... | | | | | | Great Coding. How to solve the go back "bug"
In the form.htm, add a hidden input... | | | | | | Thanks for the comments on my PHP article and for the contributions to the search... | | | | | | Dear Alejandro Gervasio,
I am a new learner of PHP and MySQL i will be... | | | | | | Dear Alejandro Gervasio
I have read your artical care fully and copy past the... | | | | | | Hi Sayed,,
Thanks for the comments on my PHP article. I took a look at your code... | | | | | | Hi again Sayed,
Thanks for your interest on my PHP article, and I really... | | | | | | Dear Sir,
I copy and past the code for webpage.php class from your... | | | | | | Hey Sayek, are you getting a 500 internal server error? Then, you should check the... | | | | | | Hi Alejandro.
Nice Tutorial!
Can you tell me where the session data is to... | | | | | | Hey Efe,
Thanks for the kind words. I’m not sure to understand entirely your... | | | | | | >>> Post your comment now! | | | | | |
|
 |