I am making a search bar in HTML/PHP/JS/MySQL. With the code I have right now, if someone enters the value book 1: conquest
into the search bar, the SQL query looks for a column value of ALL the words in the search bar.
But, If someone spells a word wrong in the search bar, or enters Conquest
instead of conquest
, they will get the dreaded no results found
result. But, every other search bar I’ve came across doesn’t suffer from this problem. Is it possible to change this?
<?php
define("DB_HOST", "localhost");
define("DB_NAME", "officiallasereyeswebsite");
define("DB_CHARSET", "utf8");
define("DB_USER", "root");
define("DB_PASSWORD", "");
$search = $_GET['search'];
$pdo = new PDO(
"mysql:host=".DB_HOST.";charset=".DB_CHARSET.";dbname=".DB_NAME,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
$stmt = $pdo->prepare("SELECT * FROM `items` WHERE `item` LIKE ? OR `code` LIKE ?");
$stmt->execute(["%".$search."%", "%".$search."%"]);
$results = $stmt->fetchAll();
if (isset($_GET["ajax"])) { echo json_encode($results); }
?>