Scenario:
So earlier, I made a request asking for ways on how to transform my four simple search queries for:
"Name", "Price", "availability" and "Category"
into one complex search that would take all of those database tables into account.
I think I managed to finally construct an SQL query that takes all of my categories into account and I tested the query in php_myadmin and it seemed to work as I needed it too.
Problem:
My problem is, I seem to be getting an error when trying to run my code, I don't think it's really anything to do with my new SQL query so I don't know what's up.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Users\Method\Desktop\MethodProject\Server\htdocs\PerfectoWebsite\SearchByAllCriteria.php on line 117
My error wants mysqli_result as a parameter and gets a boolean, although this code has worked for all my other searches thus far without error, so I'm pretty certain something before this code is resulting in an error.
I've managed to figure out the error is somewhere within this section of PHP, I'm just unsure where:
<?php include "ConnectToServer.php" ?>
<?php include "ConnectToDatabase.php" ?>
<?php
//check to make sure the form has been submitted and retrieve the contents of
//both things and make variables.
if(isset($_POST['search'])){
$get_price=$_POST['price'];
$get_function=$_POST['Operation'];
$get_name=$_POST['UserEnteredText'];
$get_Availability=$_POST['AvailabilityOperation'];
$get_Category=$_POST['CategoryOperation'];
//create the query to extract the data from the table and store
//the SQL query in a variable called $query
/*$query="SELECT * FROM friends WHERE name = '$get_name'";*/
$query="SELECT * FROM product WHERE price $get_function $get_price
AND SELECT * FROM product WHERE avaliability = $get_Availability
AND SELECT * FROM product WHERE category = $get_Category
AND SELECT * FROM product WHERE product name = $get_name";
//the result set of the mysql_query function is then stored in
//an array called $result
$result=mysqli_query($connection, $query);
//create while loop and loop through result set, reading each one
//and displaying it through the use of an echo command
>>>line 117<<<while ($row = mysqli_fetch_array($result)) {
As before, I'd greatly appreciate assistance, I'm pretty much a noob at this and I really want to understand why these errors are happening so I can prevent them in the future.
Thanks.
[Edit]:
I added:
var_dump($query);
and when I submit the search from now, I get:
string(225) "SELECT * FROM product WHERE price < '5001' UNION SELECT * FROM product WHERE availability = 'Now' UNION SELECT * FROM product WHERE category = 'Garden' UNION SELECT * FROM product WHERE product name like '%a%'"
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Users\Method\Desktop\FionaProjectMeta\Server\htdocs\PerfectoWebsite\SearchByAllCriteria.php on line 120
Still can't see the error.
[Edit 2]:
Okay, the error is in one/both these two lines:
UNION SELECT * FROM product WHERE category = '$get_Category'
UNION SELECT * FROM product WHERE product name like '%$get_name%'";
Just unsure where.
[Edit 3]:
I'm no longer getting an error, once I put 'product name' in quotes my SQL query suddenly works, however It's not doing what it's meant to be doing and limiting the results displayed on the page.