尝试使用 mysqli 创建动态 where 子句时遇到很多错误:
警告:mysqli_stmt::bind_param() 的参数 2 应为参考,在第 319 行的 ... 中给出的值
警告:mysqli_stmt::execute(): (HY000/2031): 没有为第 328 行的准备好的语句中的参数提供数据
警告:mysqli_stmt::bind_result(): (HY000/2031): No data provided for parameters in Prepared statement in ... on line 331
警告:mysqli_stmt::store_result(): (HY000/2014): 命令不同步;您现在无法在 ... 第 332 行运行此命令
我猜想解决这些问题需要进行一些更改,但是如果两个下拉菜单中的一个不相等All
或两者不相等All
,则会出现错误。
下面是显示下拉菜单和查询(带有动态 where 子句)的代码,具体取决于所选的 n 个选项:
HTML:
学生下拉菜单:
<select name="student" id="studentsDrop">
<option value="All">All</option>
<option value="11">John May</option>
<option value="23">Chris Park</option>
</select>
问题编号 下拉菜单
<select name="question" id="questionsDrop">
<option value="All">All</option>
<option value="123">1</option>
<option value="124">2</option>
<option value="125">3</option>
</select>
PHP/MYSQLI:
function StudentAnswers()
{
/*BELOW IS THE QUERY WHERE I AM TRYING TO RETRIEVE DATA DEPENDING ON THE ASSESSMENT CHOSEN AND
THEN DEPENDING ON OPTIONS CHOSEN IN STUDENT AND QUESTION NUMBER DROP DOWN MENU */
$selectedstudentanswerqry = "
SELECT
StudentAlias, StudentForename, StudentSurname, q.SessionId, QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT( DISTINCT Answer
ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
FROM Student s
INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
// Check whether a specific student was selected
if($_POST["student"] !== 'All') {
$where[] = 'sa.StudentId = ?';
$parameters[] =& $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
// NB: This is not an else if!
if($_POST["question"] !== 'All') {
$where[] = 'q.QuestionId = ?';
$parameters[] =& $_POST["question"];
$parameterTypes .= 'i';
}
// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'),
array_merge(array($parameterTypes), $parameters)); //LINE 319 ERROR 1
}
//Add group by and order by clause to query
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute(); //LINE 328 ERROR 2
//bind database fields
$selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo,
$detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
$detailsMouseClick,$detailsStudentMark); //LINE 331 ERROR 3
//store results retrieved
$selectedstudentanswerstmt->store_result(); //LINE 332 ERROR 4
//count number of rows retrieved
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
//output query
echo "$selectedstudentanswerqry";
}
?>
这是一个演示: 演示
在演示中,从下拉菜单中选择评估并提交。您将看到两个下拉菜单。将它们都设置为All
并提交,它将毫无问题地输出查询。在其中一个下拉菜单中选择否,更改All
为特定学生或问题,然后提交。现在你会看到错误
VAR转储:
当我选择带有 value 、 student number value和 question number value以及 WHERE CLAUSE的var_dump(array_merge(array($parameterTypes), $parameters)));
会话(评估)时的结果:31
40
81
WHERE q.SessionId = ? AND sa.StudentId = ? AND q.QuestionId = ?
我得到这个输出:array(4) { [0]=> string(3) "iii" [1]=> string(2) "31" [2]=> string(2) "40" [3]=> string(2) "81" }