Ok this is strange but I will explain what is going on.
In SQL lets say I want to find 2 terms (AAA, AAB) in the database using the LIKE statements, the query which is able to find the results of those terms is below:
SELECT q.QuestionContent
FROM Question q
WHERE q.QuestionContent LIKE '%AAA%'
OR q.QuestionContent LIKE '%AAB%'
GROUP BY q.QuestionId, q.SessionId
ORDER BY IF( q.QuestionContent LIKE '%AAA%', 1, 0 ) , IF( q.QuestionContent LIKE '%AAB%', 1, 0 )
So I know the SQL works. So what I want to do is include this query in MYSQLi. The only difference is that the user gets to enter in their terms in a search box and then submit the search box. So the user can enter in 1 term, 2 terms 3 terms etc, it could be any number of terms.
So below is the MYSQLi code I have created to be able to do this:
<form action="previousquestions.php" method="get">
<p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" onchange="return trim(this)" /></p>
<p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
</form>
<?php
if (isset($_GET['searchQuestion'])) {
$searchquestion = $questioncontent;
$terms = explode(" ", $searchquestion);
$questionquery = "SELECT q.QuestionContent FROM Question q WHERE ";
$i=0;
$whereArray = array();
$orderByArray = array();
$orderBySQL = "";
$paramString = "";
//loop through each term
foreach ($terms as &$each) {
$i++;
//if only 1 term entered then perform this LIKE statement
if ($i == 1){
$questionquery .= "q.QuestionContent LIKE ? ";
} else {
//If more than 1 term then add an OR statement
$questionquery .= "OR q.QuestionContent LIKE ? ";
$orderBySQL .= ",";
}
$orderBySQL .= "IF(q.QuestionContent LIKE ? ,1,0)";
$whereArray[] = "%" . $each . "%";
$orderByArray[] = "%" . $each . "%";
$paramString .= "ss";
}
$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY " . $orderBySQL;
$stmt=$mysqli->prepare($questionquery)or die($mysqli->error);
function makeValuesReferenced(&$arr){
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced(array_merge((array)$paramString,$whereArray, $orderByArray)));
$stmt->execute();
$stmt->bind_result($dbQuestionContent);
$questionnum = $stmt->num_rows();
echo $questionquery;
echo $paramString;
//OUTPUT RECORDS
if (empty($questioncontent)){
echo "Please enter in a phrase in the text box in able to search for a question";
}
else if($questionnum ==0){
echo "<p>Sorry, No Questions were found from this Search</p>";
}
else{
$output = "";
$output .= "
<table border='1' id='resulttbl'>
<tr>
<th class='questionth'>Question</th>
</tr>
";
while ($stmt->fetch()) {
$output .= "
<tr>
<td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>
</tr>";
}
$output .= " </table>";
echo $output;
}
}
?>
The problem is that if the user enters in the correct term(s), it does not display the records from the database which contains those terms. It does not output anything. This doesn't make sense because lets say that I enter in 2 terms in the search box "AAA AAB", when I echo the query and parameters, it seems correct as it outputs this:
query output:
SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY IF(q.QuestionContent LIKE ? ,1,0),IF(q.QuestionContent LIKE ? ,1,0)
parameter output:
ssss
So my question is if the query is correct and number of paramaters are correct, what is happening that causes no records appear for a successful search?
At the moment I am getting a warning which is here:
Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of elements in type definition string doesn't match number of bind variables in ...... on line 87
what needs to happen to fix this warning?