How to make a search engine that searches in multiple columns?

0

I'm working on a project with phonegap, so I work in javascript, html and css environment and working this data externally with PHP and MYSQL. I have a search engine made by ajax and that everything is perfect, it shows me what I'm looking for correctly, but the problem is that it only searches for me by name, and I would like it to search for me by name and other data, such as the brand of the product. .

Imagine that it is Milk Whole the name, and the brand is Hacendado, because when introducing Leche Entera Hacendado appeared, at the moment it only works looking for Whole Milk.

The text entered sends it via ajax to the php file of the server, and it returns the data.

The PHP is this:

<?php
include "db.php";
$keyword=htmlspecialchars($_GET["search"]);
$data=array();
$q=mysqli_query($con,"SELECT products.product_name,brands.brand_name,products.product_price,products.product_size,products.product_image FROM products,brands WHERE brands.brand_id=products.product_brand and products.product_name LIKE '%$keyword%' and brands.brand_name LIMIT 5");
while ($row=mysqli_fetch_object($q)){
    $data[]=$row;
}
echo json_encode($data);
    ?>

Thanks in advance!

    
asked by alexdemons2000 26.02.2018 в 15:18
source

1 answer

1

If I have understood the question correctly, you are searching with the same word / filter, search on different fields of your table.

The SQL query would essentially be the same, the only thing we would add to WHERE plus clauses:

SELECT products.product_name,brands.brand_name,products.product_price,products.product_size,  
products.product_image   
FROM products,brands 
WHERE brands.brand_id=products.product_brand AND( 
  products.product_name LIKE '%$keyword%' OR
  products.nombre_campo LIKE '%$keyword%' OR
  products.nombre_campo2 Like '%$keyword%' OR 
  n campos) AND
brands.brand_name LIMIT 5;

We encapsulate the different columns between parenthesis and with OR to bring you the results if one of the fields you filter with matches the keyword, if you want it to coincide with all of them, you should replace the OR by AND

    
answered by 26.02.2018 в 15:38