Search code examples
mysqlsoundex

How to properly use soundex() in mysql


Hello I am practicing to build an e-commerce site using vanilla PHP , I want that when I search for a certain item in search bar even if there is a typo such "trffic" it can still found the item traffic. The code below seems to only work for correct spelling and incomplete words.

 <?php session_start();
    $searchbar=$_POST['searchbar']

    include 'includes/db_config.php';

    $query ="SELECT items.id, items.product_name , items.price , categories.id, 
 items.image , categories.description, items.category_id FROM items INNER JOIN categories ON items.category_id = categories.id WHERE product_name LIKE '%$searchbar%' OR description LIKE '%searchbar%'"

    ?>

Solution

  • try this way your query

     $query ="SELECT items.id, items.product_name , items.price , categories.id, 
     items.image , categories.description, items.category_id FROM items INNER JOIN categories ON items.category_id = categories.id WHERE soundex(product_name)=soundex("+$searchbar+") OR soundex(description) = soundex("+$searchbar+")";