Building a Search Engine with MySQL and PHP 5 - Performing searches with MySQL and PHP 5 (
Page 3 of 4 )
As you might have guessed, developing the business logic that drives this MySQL-based search application is reduced to defining a couple of PHP classes. These classes will be responsible for performing some crucial tasks, such as connecting to MySQL and executing a specific SELECT statement, as well as returning to the browser the corresponding database results in accordance with the search terms entered on the online form that you saw previously.
To perform all of the aforementioned tasks, below I defined two basic PHP classes. You'll probably find them quite familiar, since I have used them with some of my previous articles on PHP development published here on the prestigious Developer Shed network.
The classes in question have been wrapped into one single file, called "mysql.php", whose signature looks like this:
(definition of "mysql.php" file)
<?php
// define 'MySQL' class
class MySQL{
private $conId;
private $host;
private $user;
private $password;
private $database;
private $result;
const OPTIONS=4;
public function __construct($options=array()){
if(count($options)!=self::OPTIONS){
throw new Exception('Invalid number of connection parameters');
}
foreach($options as $parameter=>$value){
if(!$value){
throw new Exception('Invalid parameter '.$parameter);
}
$this->{$parameter}=$value;
}
$this->connectDB();
}
// connect to MySQL
private function connectDB(){
if(!$this->conId=mysql_connect($this->host,$this->user,$this-
>password)){
throw new Exception('Error connecting to the server');
}
if(!mysql_select_db($this->database,$this->conId)){
throw new Exception('Error selecting database');
}
}
// run query
public function query($query){
if(!$this->result=mysql_query($query,$this->conId)){
throw new Exception('Error performing query '.$query);
}
return new Result($this,$this->result);
}
public function escapeString($value){
return mysql_escape_string($value);
}
}
// define 'Result' class
class Result {
private $mysql;
private $result;
public function __construct(&$mysql,$result){
$this->mysql=&$mysql;
$this->result=$result;
}
// fetch row
public function fetchRow(){
return mysql_fetch_assoc($this->result);
}
// count rows
public function countRows(){
if(!$rows=mysql_num_rows($this->result)){
return false;
}
return $rows;
}
// count affected rows
public function countAffectedRows(){
if(!$rows=mysql_affected_rows($this->mysql->conId)){
throw new Exception('Error counting affected rows');
}
return $rows;
}
// get ID form last-inserted row
public function getInsertID(){
if(!$id=mysql_insert_id($this->mysql->conId)){
throw new Exception('Error getting ID');
}
return $id;
}
// seek row
public function seekRow($row=0){
if(!is_int($row)||$row<0){
throw new Exception('Invalid result set offset');
}
if(!mysql_data_seek($this->result,$row)){
throw new Exception('Error seeking data');
}
}
}
?>
As illustrated above, the previous PHP file uses a pair of MySQL processing classes to perform a search process against one or more selected databases. Also, you should notice that the versatile and extensible structure offered by the above PHP classes makes it really easy to implement an internal search engine on any existing web site, a feature that speaks for itself about the expandability of this MySQL-based search application.
So far, so good. At this time you hopefully grasped the business logic that stands behind this search engine, since it only requires a few simple PHP classes to perform the corresponding searches on the MySQL server. So what's the next step?
Well, assuming that you may want to see how all the previously defined files can be linked with each other to implement a fully-functional search application, in the following section I'm going to develop an illustrative example to show you how to put this MySQL-based search engine to work quickly.
To learn how this hands-on example will be developed, please click on the link that appears below and keep reading.
| | 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! | | | | | |
|
 |