I have a search box. It uses explode to create an array and uses it to look for the items from the database (3 columns - name, description, tags). This version works fine (single column - name):
function searchResults(){
$con = mysqli_connect("localhost", "root", "", "cookbook");
$search = $_POST["search-bar"];
$data = explode(' ',$search);
$nameloop = "";
$i = 1;
while ($i < 10){
if(!empty($data[$i])){
$nameloop .= " AND name LIKE '%".$data[$i]."%' ";
}
$i++;
}
echo "<br>".$nameloop."<br>";
$sql = "
SELECT * FROM product WHERE
name LIKE '%$data[0]%'".$nameloop." ORDER BY name
";
echo $sql;
$res = mysqli_query($con,$sql);
$counter = 0;
$limit = 8;
while($row = mysqli_fetch_assoc($res) and ($counter < $limit)){
--> CONTENT <--
$counter++;
}
mysqli_close($con);
}
When I'm trying to do it for all 3 columns with mysqli_multi_query:
function searchResults(){
$con = mysqli_connect("localhost", "root", "", "cookbook");
$search = $_POST["search-bar"];
$data = explode(' ',$search);
$nameloop = "";
$descloop = "";
$tagsloop = "";
$i = 1;
while ($i < 10){
if(!empty($data[$i])){
$nameloop .= " AND name LIKE '%".$data[$i]."%' ";
}
$i++;
}
echo "<br>".$nameloop."<br>";
$i = 1;
while ($i < 10){
if(!empty($data[$i])){
$descloop .= " AND description LIKE '%".$data[$i]."%' ";
}
$i++;
}
echo "<br>".$descloop."<br>";
$i = 1;
while ($i < 10){
if(!empty($data[$i])){
$tagsloop .= " AND description LIKE '%".$data[$i]."%' ";
}
$i++;
}
echo "<br>".$tagsloop."<br>";
$sql = "
SELECT * FROM product WHERE
name LIKE '%$data[0]%'".$nameloop." ORDER BY name;
SELECT * FROM product WHERE
description LIKE '%$data[0]%'".$descloop." ORDER BY name;
SELECT * FROM product WHERE
tags LIKE '%$data[0]%'".$tagsloop." ORDER BY name;
";
echo $sql;
$res = mysqli_multi_query($con,$sql);
$counter = 0;
$limit = 8;
while($row = mysqli_fetch_assoc($res) and ($counter < $limit)){
--> CONTENT <--
$counter++;
}
mysqli_close($con);
}
Error:
: Uncaught TypeError: mysqli_fetch_assoc(): Argument #1 ($result) must be of type mysqli_result, bool given in C:\xampp\htdocs\Php\search-bar-modify-recipes.php:66
Stack trace:
#0 C:\xampp\htdocs\Php\search-bar-modify-recipes.php(66): mysqli_fetch_assoc(true)
#1 C:\xampp\htdocs\Php\search-bar-modify-recipes.php(89): searchResults()
#2 {main}
thrown in [file path] on line 66 [while($row = mysqli_fetch_assoc($res)...]
To clarify - I want the search box to look for the words from the input in the database like this:
SELECT * FROM product WHERE name LIKE 'pi' AND name like 'sal' OR description LIKE 'moz' AND 'tor' ORDER BY name
So it should output database items (name: salami pizza | desc: tomato sauce, pesto), (name: mozzarella wrap | desc: mozzarella and a big tortilla). I hope it's clear. When I echo $sql, the query seems fine:
SELECT * FROM product WHERE
name LIKE '%piz%' ORDER BY name;
SELECT * FROM product WHERE
description LIKE '%piz%' ORDER BY name;
SELECT * FROM product WHERE
tags LIKE '%piz%' ORDER BY name;
Will it even work when it's fixed? Will it be 1 list of items or 3 lists (I mean the ORDER BY element, I want all of them to be sorted in 1 list)? How can I fix it? EDIT: It's not a duplicate. That post refers to searching in 1 column at a time using multiple keywords, I want to search in all three columns at once, that's why OR or UNION don't work in this case.