Building a Search Engine with MySQL and PHP 5 - Assembling the modules of the MySQL-driven search application (
Page 4 of 4 )
As I stated in the section that you just read, implementing this MySQL-driven search engine is a matter of fetching the search terms entered on the online web form, and then embedding them into a SELECT query, and finally performing the query in question against one or more selected databases.
Logically, the last step involved in this process is to display the corresponding results (assuming that the query was successful) on the browser. Quite simple, right?
All right, based upon this description of how the search application must work, below I listed the definition of a new PHP file. It is called "processform.php" and is tasked first with performing a search process against a sample "USERS" database table, and then showing the result to the end user.
Given that, the signature of this brand new PHP file is as follows:
(definition for "processform.php" file)
<?php
// include MySQL-processing classes
require_once 'mysql.php';
try{
// connect to MySQL
$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
$searchterm=$db->escapeString($_GET['searchterm']);
$result=$db->query("SELECT firstname, lastname,comments FROM
users WHERE MATCH(firstname,lastname,comments) AGAINST
('$searchterm')");
if(!$result->countRows()){
echo '<div class="maincontainer"><h2>No results were found. Go
back and try a new search.</h2></div>'."n";
}
else{
// display search results
echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";
while($row=$result->fetchRow()){
echo '<div class="rowcontainer"><p><strong>First Name:
</strong>'.$row['firstname'].'<p><p><strong>Last Name:
</strong>'.$row['lastname'].'</p><p><strong>Comments:
</strong>'.$row['comments'].'</p></div>'."n";
}
}
echo '</div>';
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
As you can see, the above PHP file uses the two MySQL processing classes to perform the pertinent search process against the "USERS" database table. In this case, I built the search query using the "MATCH" and "AGAINST" clauses to take advantage of full-text capabilities offered by newer versions of MySQL, but you may want to use a more conventional "LIKE" statement, particularly if your search queries are rather primitive.
And finally, assuming that the aforementioned "USERS" database table has been previously populated 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
Please take a look at the following screen shots. They show the database results returned by this MySQL-driven search application when the search strings "Alejandro," "Alejandro+Susan" and finally "Alejandro+Susan+John" are entered into the corresponding online web form:



As demonstrated clearly by the above images, building an expandable search application using the powerful MySQL/PHP 5 combination is indeed a no-brainer process that can be performed with minor efforts from us. Feel free to use all the source files shown here to implement this search engine on your own web site, so you can provide users with the possibility of searching the site contents.
Final thoughts
In this first part of the series, you hopefully learned how to create an expandable search application using the capabilities offered by MySQL and PHP 5. Nonetheless, this instructive journey isn't finished yet, since the application lacks important features, such as the implementation of true Boolean searches and results paging.
All of these improvements will be introduced in the next part of the series, so you don't have any excuses to miss it!
| | Discuss Building a Search Engine with MySQL and PHP 5 | | | | | | | In this first oart of the series, the basic structure of this PHP 5-based search... | | | | | | Is there a way to weight the results? For example, if the 'firstname' col is a... | | | | | | Thank you for commenting on my PHP article. Regarding your question, it's possible... | | | | | | Excellent, thank you.
(i will probably have some questions for you on that... | | | | | | Again, thank you for your comments.Regards. | | | | | | Again, thank you for your comments.
Regards. | | | | | | This system is WAY too complicated. It really doesn't need to be this complex.
How... | | | | | | Thank you for commenting on my PHP article. Yes, the code sample you listed... | | | | | | I'm not getting any result out of this code and I don't find the assignment for... | | | | | | Thank you for commenting on my PHP article. Actually, this search engine works just... | | | | | | Thanx I knew what's going on now actually it's my fault cause I made a dump mistake... | | | | | | Thank you for the comments on my PHP article. And I'm glad to know you fixed up the... | | | | | | I have all the code as shown and the MySQL is the right type etc, but when I click... | | | | | | Thank you for commenting on my PHP article. Concerning your question, as I expressed... | | | | | | Thank you for writing this tutorial. I am still learning about classes and don't... | | | | | | very nice articles. simple, effective and extremely useful. it's exactly what our... | | | | | | I get this error when I enter a text and press search: "Error performing query... | | | | | | Thank you for commenting on my PHP article. Indeed, the search engine works just... | | | | | | Hi there,
I get the following error message when trying this script:
Warning:... | | | | | | Thank you for commenting on my PHP article. As any other application that uses... | | | | | | Hi I am getting this parse error when trying to run a search.
Parse error: syntax... | | | | | | Thank you for the comments on my PHP article. Concerning your question, the... | | | | | | I agree with you that it can't be your code... has to be me.. as I am not that... | | | | | | Hey Tony,
Thanks again for the comments. It’s clear now what's going on. You’re... | | | | | | Thank you,
ok, I have updated the php version to 5.2.5 as you can see... | | | | | | Hey again Tony,
Now, seems that the source code of all the classes is correct... | | | | | | hi,
I'm also getting the same problem:
"Error performing query SELECT... | | | | | | Hi Sophia,
Thanks for the comments on my PHP article. As I posted before, here’re... | | | | | | I modified a little your code.. when i click the button "search" i see this message... | | | | | | Thanks for commenting on my PHP article. Now, concerning your question, you’re... | | | | | | First i would like to appreciate your effort for writing this article, What i want... | | | | | | I'm here again.
After i supplied my (Host, User, Database & Password )
when o... | | | | | | As I posted previously, the search engine works just fine, and you’re getting that... | | | | | | Thanks for commenting on my PHP article. Yes, you must replace the parameters passed... | | | | | | Hi All,
Please send me any one who has all files checked and free of... | | | | | | I am facing the same problem I have checked all possible reasons as you prescribed... | | | | | | Dear Alejandro Gervasio,
I need to read your complete articles of... | | | | | | Hi again Sayed,
Thanks for the kind words. Here’re the links you requested for my... | | | | | | Hi Sayed,
I already included at the bottom of the page the list of links of each... | | | | | | Great script!!, learning a lot.. can you please help me with this error,
i have... | | | | | | Thanks for the kind words on my PHP search engine. Concerning your question, make... | | | | | | Thank you Mr. Gervasio for great article - it finally works.
I've been trying for... | | | | | | Thanks for the words on my article. Good to know you've fixed up the copy/paste... | | | | | | This is a great tutorial that you have written. I followed on the instructions and I... | | | | | | I cannot seem to find the links at the bottom of the page. Where are they? | | | | | | Hey,
Good to know you liked my PHP article. Regarding your question, you should... | | | | | | Hi this is a very good article for freshers like me...i followed the same procedure... | | | | | | Thanks for the comments. As I mentioned in previous posts, the error you’re getting... | | | | | | Hi
I get fixed this error.Instead of using
SELECT * FROM motors WHERE... | | | | | | Hi, great article.
I am trying to build a search function whereby the user types... | | | | | | Thanks for the comments. Regarding your question, here’s a simple procedural example... | | | | | | For those who are getting "Error performing query SELECT" error, do following to... | | | | | | >>> Post your comment now! | | | | | |
|
 |