I have the following search snippet and it works when I only use it with the value1 and value2 data from mysql table.
When I add Mysql CASE Function
to the query with value1r and value2r as in the example below, and try to make a search on the page, it gives me this error "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...."
which means I have a mistake in the query.
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM gdata
WHERE
Value1 = '" . $valueToSearch . "' OR
Value2 = '" . $valueToSearch . "' OR
Value1r = '" . $valueToSearch . "' OR
Value2r = '" . $valueToSearch . "'
";
$search_result = filterTable($query);
}
else {
$query = "SELECT Value1, Value2,
CASE
WHEN Value1 >= 90 AND Value1 <= 100
THEN 'A'
WHEN Value1 >= 80 AND Value1 <= 89
THEN 'B'
WHEN Value1 >= 70 AND Value1 <= 79
THEN 'C'
WHEN Value1 >= 0 AND Value1 <= 69
THEN 'F'
ELSE '' END AS Value1r,
CASE
WHEN Value2 >= 90 AND Value2 <= 100
THEN 'A'
WHEN Value2 >= 80 AND Value2 <= 89
THEN 'B'
WHEN Value2 >= 70 AND Value2 <= 79
THEN 'C'
WHEN Value2 >= 0 AND Value2 <= 69
THEN 'F'
ELSE '' END AS Value2r
FROM gdata";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Gdata</title>
</head>
<body>
<form action="gdata.php" method="post">
<input type="text" name="valueToSearch" placeholder="Search">
<input type="submit" name="search" value="Filter">
<table>
<tr>
<th>G1</th>
<th>G2</th>
<th>R1</th>
<th>R2</th>
</tr>
<?php
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" . $row['Value1'] . "</td>";
echo "<td>" . $row['Value2'] . "</td>";
echo "<td>" . $row['Value1r'] . "</td>";
echo "<td>" . $row['Value2r'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
</body>
</html>
What am I doing wrong here? Can I get a little help please? thanks!
Note: My question is not about the given error specifically.