I am building a MySQLi full text search with multiple values in IN() using prepared statements.
The script is working fine for single value in IN() but not for multiple values.
Code
$keyword = "+$keyword*";
$search = 'Games';
$query = "SELECT id, title FROM $tablename WHERE category IN(?) AND MATCH (title) AGAINST ( ? IN BOOLEAN MODE)";
$prepare = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($prepare, "ss", $search, $keyword);
mysqli_stmt_execute($prepare);
mysqli_stmt_bind_result($prepare, $id, $title);
while(mysqli_stmt_fetch($prepare)){
echo $id.' '.$title.'<br />';
}
mysqli_stmt_close($prepare);
The above code does not work for multiple values like.
$search = "'Software', 'Games'";
I guess I need to do something like.
$IN = rtrim(str_repeat('?, ', count($search)), ', ') ;
$query = "SELECT id, title FROM $tablename WHERE category IN($IN) AND MATCH (title) AGAINST ( ? IN BOOLEAN MODE) ";
Now how to handle these parts.
mysqli_stmt_bind_param($prepare, "ss", $search, $keyword);
mysqli_stmt_execute($prepare);
As binding parameters increases with multiple values.
Please see and suggest any possible way to do this.
Thanks.
Update
I tried like this
$search = array('Software', 'Games');
$IN = rtrim(str_repeat('?, ', count($search)), ', ') ;
$search[]=$keyword;
$query = "SELECT id, title FROM $tablename WHERE category IN($IN) AND MATCH (title) AGAINST ( ? IN BOOLEAN MODE) ";
$prepare = mysqli_prepare($connection, $query);
mysqli_stmt_execute($search);
mysqli_stmt_bind_result($prepare, $id, $title);
while(mysqli_stmt_fetch($prepare)){
echo $id.' '.$title.'<br />';
}
mysqli_stmt_close($prepare);
But it didnt work, It gives me error mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, array given