I am trying to make a live search for my website.But the mysqli prepared statement for getting the results is not working. I have the following code:
$value = $_POST['value'];
$query = $db->prepare("
select title from forums where title IN(?)
");
$value = explode(' ',$value);
$value = "'" . implode("','", $value) . "'";
$query -> bind_param('s',$value);
$query -> execute();
$query -> store_result();
$number = $query -> num_rows();
if($number == 0){
echo "No results found.";
}else{
$query -> bind_result($title);
while($query -> fetch()){
echo $title.'<hr>';
}
I dont know how many words there are going to be in the search phrase so i cannot use:
IN(?,?,?,...)
I have a record in my forums table with title 'This is a test forum'. But When I type the name the result does not show. Am i doing the prepared statement correctly.Please help
UPDATE
Thanks to all who have answered and commented. I finally ended up using the following code:
$value = $_POST['value'];
$query = $db->prepare("
select title from forums where match(title) against(?) OR name LIKE CONCAT('%',?,'%')
");
$query -> bind_param('ss',$value,$value);
$query -> execute();
$query -> store_result();
$numrows = $query -> num_rows();
if($numrows == 0){
echo "No results found";
}
else{
$query -> bind_result($title);
while($query -> fetch()){
echo '<hr>';
echo $title;
echo '<hr>';
}
}