This question is similar to this previously asked question except that I am using mysqli instead of PDO.
I perform the query as such:
$word=preg_split('/[\s]+/',$terms);
$totalwords=count($word);
$terms is obtained further up from a GET
$sql="SELECT title,content FROM articles WHERE (title LIKE CONCAT('%',?,'%') OR (content LIKE CONCAT('%',?,'%'))";
for(i=1;$i<$totalwords;$i++){
$sql.=" OR (title LIKE CONCAT('%',?,'%') OR (content LIKE CONCAT('%',?,'%'))";
}
$stmt=$conn->prepare($sql);
foreach($word as $key => $keyword){
$term=$keyword;
$stmt->bind_param('ss',$term,$term);
}
$stmt->execute;
$stmt->store_result;
If I enter one word in the search form, I get the right results, however, if I enter more than one word, I get no results at all.
So I included a print_r($sql);
to see what was being sent to the database and this is what I get for one term (which works fine):
SELECT title,content FROM articles WHERE (title LIKE CONCAT('%',?,'%') OR content LIKE CONCAT('%',?,'%'));
and this is what I get for multiple words (which returns no results even though it should):
SELECT title,content FROM articles WHERE (title LIKE CONCAT('%',?,'%') OR content LIKE CONCAT('%',?,'%')) OR (title LIKE CONCAT('%',?,'%') OR content LIKE CONCAT('%',?,'%'));
which seems like everything is working fine, so I turned on my mysql logs and discovered that the single word queries show up but the multi-word queries are non-existent.
So I added print_r($stmt);
and I get Commands out of sync; you can't run this command now
for multi-word queries.
What am I doing wrong?