mysqli を使用して動的 where 句を作成しようとすると、かなりの数のエラーが発生します。
警告: mysqli_stmt::bind_param() へのパラメータ 2 は参照であると予想されます。値は 319 行目の ... で指定されます
警告: mysqli_stmt::execute(): (HY000/2031): 328 行目の ... の準備済みステートメントのパラメーターにデータが提供されていません
問題を解決するために必要な変更があると思いますが、2 つのドロップダウン メニューのいずれかが等しくないAll
場合、または両方が等しくない場合All
、エラーが発生します。
以下は、選択された n オプションに応じて、ドロップダウン メニューとクエリ (動的 where 句を使用) の両方を表示するコードです。
PHP/MYSQL:
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";
}
?>