CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1

    Search mySQL DB with similar words

    I have a search form that searches for product in my table. Lets say a customer searches for "PHP Help Guide" it will populate the result for my product that is named "PHP Help Guide" Also if the user searches "PHP Help" It will show all results that are for that search term like such:
    PHP Help Guide
    PHP Help Tutorial
    PHP Help Forums
    PHP Help you get the picture

    But if the user searches for PHP Guide it does not show the PHP Help Guide in the result and because nothing is Named PHP Guide no data shows.

    How can I have my following code search my DB and show results for all words in the search form that are similar in nature?

    Code:
    <?php
    
    include '../config/config.php';
    
    ############## Make the mysql connection ###########
            try {
                    $db = new PDO("mysql:host=$hostname;dbname=SnyderLanceSku", $username, $password);            
                    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                    
                    $block1 = "block 1";
                    $block2 = "block 3";
                    $block3 = "block 1";
                    $i_d = $_POST['value'];
    
    ////Inserts Search Data////////
    $sql = "INSERT INTO `keyword` (`keyword`)
    VALUES('".$_POST['value']."')";
    /////////END///////////////////
      
     ////////This is the main table for keywords//////////       
        $count = $db->exec($sql);
            $sql = "Select * from products WHERE Name LIKE '%" . $i_d . "%' OR UPC LIKE '%" . $i_d . "%'  ";
        $result = $db->query($sql);
    				
                    echo "<table id='products' width='400px'>";
    					echo'<tr id="top" style="background-color:#fff111"> ';
    						echo"<td>Product #</td>";
    							echo"<td id='name'>Name</td>";
    								echo"<td>Image</td>";
    							echo"<td>Retail Price</td>";
    						echo"<td>Ounces</td>";
    					echo"<td>Case Count</td>";
    				echo"<td>UPC</td>";
    				
                    foreach ($result as $data) {
                            echo ' 
                            <tr style="background-color:#690b06;"> 
                            <td id="key">'.$data["Product_sku"].'</td> 
                            <td id="name"><a target="_blank" href="http://www.upcindex.com/'.$data["UPC"].'">'.$data["Name"].'</td> 
    						
    						
                           <td id="bl">
    					   
    		                                  <img src="http://morinandsons.com/search/images/'.$data["Name"].'.jpg" width="50" height="65" class="image">
    					   
    					   </td>
    						<td id="case">'.$data["Suggested_Retail"].'</td> 
    						<td id="ounces">'.$data["Item_Ounces"].'</td> 
                            <td id="retail">'.$data["Case_Pack"].'</td>
    					    <td id="name"><a target="_blank" href="https://itemmaster.com/search_field:'.$data["UPC"].'">'.$data["UPC"].'</td> 
    						<td align="center" bgcolor="#006600"><a href="delete.php?id='.$data["keyword"].'" onclick="return confirm(\'This action cannot be reversed. Are you sure you want to delete?\')">Delete</a></td>
                            <td align="center" bgcolor="#006600"><a href="update.php?id='.$data["Product_sku"].'&name='.$data["Name"].'&retail='.$data["Suggested_Retail"].'&ounces='.$data["Item_Ounces"].'&upc='.$data["UPC"].' ">update</a></td>';
                        
                            
                            
                    }
                    echo "</tr></table>";
    
    $db = null; // close the database connection
                    
            }
            catch(PDOException $e) {
                    echo $e->getMessage();
            }
            
            ///////// END MAIN TABLE/////////////////////////       
    		
    		
    ?>
    New to PHP and MySql. Started looking at scripts about a week ago. So far have very little understanding of it. So please be easy with me if I ask a stupid question.

    www.ethans-space.com

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Search mySQL DB with similar words

    Have you tried creating individual WHERE...LIKE statements for each word in the user submitted search?

    Untested, but looks right.
    PHP Code:
    $arrWhere = array();
    $words explode(' '$_POST['value']);
    foreach (
    $words as $word) {
      
    $arrWhere[] = "Name LIKE '" $word "'"
    }
    $strWhere implode(' OR '$arrWhere); 
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured