MySQL
  Home arrow MySQL arrow Page 4 - Using Boolean Operators for Full Text ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Using Boolean Operators for Full Text and Boolean Searches with MySQL
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-06-20

    Table of Contents:
  • Using Boolean Operators for Full Text and Boolean Searches with MySQL
  • Reintroducing some earlier concepts
  • Using the plus operator
  • Using the minus operator

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    Using Boolean Operators for Full Text and Boolean Searches with MySQL - Using the minus operator
    (Page 4 of 4 )

    As I explained in the previous section, the last example that I'm going to show you in this article surrounding the implementation of Boolean searches with MySQL is based upon using the minus (-) operator. This operator is useful for discarding specific terms from a particular search string.

    To demonstrate a simple usage of this handy operator, first I'm going to list the two source files that you learned in the previous section, which look like this:

    (definition of form.htm file)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Testing Boolean searches using the minus (-) operator</title>
    <style type="text/css">
    body{
      
    padding: 0;
      
    margin: 0;
      
    background: #fff;
    }

    h1{
      
    font: bold 16px Arial, Helvetica, sans-serif;
       color: #000;
      
    text-align: center;
    }

    p{
      
    font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
      
    color: #000;
    }

    #formcontainer{
      
    width: 40%;
      
    padding: 10px;
      
    margin-left: auto;
      
    margin-right: auto;
      
    background: #6cf;
    }
    </style>
    </head>
    <body>
     
    <h1>Testing boolean searches using the minus (-) operator</h1>
     
    <div id="formcontainer">
       
    <form action="search.php" method="get">
         
    <p>Enter search term here : <input type="text"
    name="searchterm" title="Enter search term here" /><input
    type="submit" name="search" value="Search Now!" /></p>
       
    </form>
      
    </div>
    </body>
    </html>

    (definition of search.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');
        
    }
      
    }
    }
    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 FROM users WHERE MATCH
    (firstname,lastname,comments) AGAINST('$searchterm' IN BOOLEAN
    MODE)");
      
    if(!$result->countRows()){
        
    echo 'No results were found.';
      
    }
      
    else{
        
    echo '<h2>Users returned are the following:</h2>';
        
    while($row=$result->fetchRow()){
          
    echo '<p>Name: '.$row['firstname'].' Relevance: '.$row
    ['relevance'].'</p>';
        
    }
      
    }
    }
    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }
    ?>

    All right, having listed the respective signatures of the above source files, you should notice that the corresponding SELECT query remains completely the same, meaning that the query in question also uses the "IN BOOLEAN MODE" statement.

    That being said, here are the results returned by MySQL when the search string "MySQL -database" is entered in the respective web form:

    /*
    Users returned are the following:

    // displays the following entering 'MySQL -database' search term
    /*
    Users returned are the following:

    Name: Alejandro

    */

    In this case, the (-) operator has been used to return all the database rows containing the word "MySQL" but not including the term "database." As you can see by the group of basic examples shown here, by using the "IN BOOLEAN MODE" statement it's possible to build quickly a MySQL-driven search engine that supports full text and Boolean searches.

    Final thoughts

    Sadly, we've come to the end of this series. Hopefully, after examining all of the code samples included in these tutorials, you'll have a much more solid background in how to implement full text and Boolean searches with MySQL.

    See you in the next PHP tutorial!


    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.

       · Over the course of this final part of the series, you'll learn how to use boolean...
     

       

    MYSQL ARTICLES

    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway