I'm trying to query a database containing information on animals, doing this by searching the table's records for either the animal, or animal category. For example, input could be "Lion" searched in the AnimalName field, or "Mammal" in the Category field. However, while my code accurately reads the value entered and stores it using $searchItem as desired, it does not receive a response from the database, even if the data entered matches a record. It will always return no result (stored in $result), regardless of what is searched.
Some help in finding out what is wrong with my code would be greatly appreciated, if any more information is needed please ask. Code run when data is input, note that the 'input_type' stores whether the search is being made based on AnimalName or Category, and 'searched_item' stores the string being searched for:
<?php
/*The "animaldatabase" database is being accessed, with the "animal" table being queried.*/
$searchType = $_GET['input_type'];
$searchItem = "'" . filter_var($_GET['searchedItem'], FILTER_SANITIZE_STRING) . "'";
$host="localhost";
$username="root";
$pword="root";
$database="animaldatabase";
require("db_connection.php");
if($searchType == "animal"){
$sql="SELECT * FROM animaltable WHERE AnimalName = $searchItem";
}
if($searchType == "category"){
$sql="SELECT * FROM animaltable WHERE Category = $searchItem";
}
$result = $mysqli->query($sql);
And the linked database connection code:
<?php
//connect to the database using mysqli API
$mysqli = new mysqli($host, $username, $pword, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" .
$mysqli->connect_errno . ") "
. $mysqli->connect_error;
die;
}
?>