I have a regular query working for the search function I'm implementing but I'm trying to implement it now by using prepared statements. I have used prepared statements before just fine but never with wildcards.
I have tried a lot of the possible solutions I have found on this site like: using CONCAT('%',?,'%) or $searchterm = "%{$searchterm}%"; or using get_result() instead of bind_result and just can't seem to get it working.
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$stmt = $dbconn->prepare("SELECT name, price, id, img FROM product WHERE ? LIKE CONCAT('%',?,'%')");
$stmt->bind_param("ss", $searchtype, $searchterm);
$stmt->execute();
$stmt->bind_result($name, $price, $id, $img);
and have tried this
$searchterm = "%{$searchterm}%";
$stmt = $dbconn->prepare("SELECT name, price, id, img FROM product WHERE ? LIKE ?");
$stmt->bind_param("ss", $searchtype, $searchterm);
$stmt->execute();
$results = $stmt->get_result();
$count = $results->num_rows;
$row = $results->fetch_array();
I've tried many simple searches which work fine with the original non-prepared stmt query of my search function, but no matter what I try with my prepared stmt I get no results.
Sorry, your search returned no results. name met 0 Above name is the search type and met is search term using CONCAT and count is getting no results.
Sorry, your search returned no results. name %met% 0 This is using $searchterm = "%{$searchterm}%";
I'm not an experienced coder by any means but browsing SO usually solves any issues I'm having but right now nothing is working. What am I missing?