Building the Server-side Component of a Search Engine with AJAX - Putting the search engine to work
(Page 4 of 4 )
As I expressed in the section that you just read, implementing this AJAX-driven search engine is only a mater of coding a basic PHP script in the server, since all the hard work of doing searches is performed by AJAX.
At this stage, I'm quite sure you're asking the following question: how does the application retrieve the corresponding results from the sample "pages" database table, in accordance with a specified search term? Well, fortunately the answer is rather simple if you take a look at the following PHP script, which not only searches in the mentioned database, but sends the returned results right back to the client.
try{
//connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user',
'password'=>'password','database'=>'database'));
$searchterm=mysql_escape_string($_GET['searchterm']);
$result=$db->query("SELECT * FROM pages WHERE contents
LIKE '%$searchterm%' ORDER BY id ASC");
echo '<h2>'.$result->countRows().' records matched your search
criteria.</h2>';
echo '<ul>';
while($row=$result->fetchRow()){
$row['centercol']=str_replace
($searchterm,'<strong>'.$searchterm.'</strong>',$row['contents']);
echo '<li><a
href="http://www.mywebsite.com/index.php?id='.$row['id'].'">'
.$row['title'].'</a><p>'.$row['contents'].'</p></li>';
}
echo '</ul>';
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As you can see, the above code snippet first connects to MySQL, then performs the corresponding search by using the popular LIKE statement, and finally displays the returned result (if any) straight to the browser. It's short and simple!
For this case in particular, I decided to use a LIKE command. If you're working with large databases, this isn't the best approach to follow. Instead you should use FULL-TEXT searches. I'm getting ahead of myself, since that will be the subject of the final part of the series.
For now, try out the PHP script that I provided and enjoy incorporating this fully-functional AJAX-driven search engine into your own web applications.
Final thoughts
In this second part of the series, I showed you how to complete this AJAX-based search engine by defining a pair of PHP classes that actually perform searches against a selected database. While this approach will work with small websites, it's true that it can be greatly improved by implementing some handy features, such as FULL-TEXT and BOOLEAN searches, which are also supported by MySQL.
That will be exactly the topic covered in the final installment of this series, thus you don't have any excuses to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |