[英]getting errors when creating a dynamic where clause
嘗試使用mysqli創建動態where子句時,出現很多錯誤:
警告:mysqli_stmt :: bind_param():類型定義字符串中的元素數量與第318行上...中的綁定變量數量不匹配
警告:mysqli_stmt :: execute():(HY000 / 2031):在第327行的...中,未提供准備好的語句中的參數數據
警告:mysqli_stmt :: bind_result():(HY000 / 2031):第330行上的...中的准備好的語句中未提供參數的數據
警告:mysqli_stmt :: store_result():(HY000 / 2014):命令不同步; 您現在無法在第331行的...中運行此命令
我猜想需要解決一些小的問題,但是發生的情況是,如果兩個下拉菜單中的一個不等於All
或者如果兩個都不等於All
那么就會出現錯誤。
下面的代碼顯示了下拉菜單和取決於所選擇的n個選項的查詢(帶有動態where子句):
function ShowAssessment()
{
$studentactive = 1;
$currentstudentqry = "
SELECT
st.StudentId, st.StudentAlias, st.StudentForename, st.StudentSurname
FROM
Student_Session ss
INNER JOIN
Student st ON ss.StudentId = st.StudentId
WHERE
(ss.SessionId = ? and st.Active = ?)
ORDER BY st.StudentAlias
";
global $mysqli;
$currentstudentstmt=$mysqli->prepare($currentstudentqry);
// You only need to call bind_param once
$currentstudentstmt->bind_param("ii",$_POST["session"], $studentactive);
// get result and assign variables (prefix with db)
$currentstudentstmt->execute();
$currentstudentstmt->bind_result($dbStudentId,$dbStudentAlias,$dbStudentForename,$dbStudentSurname);
$currentstudentstmt->store_result();
$studentnum = $currentstudentstmt->num_rows();
if($studentnum == 0){ ?>
<div class="red">
There are no Students who have currently taken this Assessment
</div>
<?php } else {
$questionsqry = "
SELECT
QuestionId, QuestionNo
FROM
Question
WHERE
(SessionId = ?)
ORDER BY QuestionNo
";
global $mysqli;
$questionsstmt=$mysqli->prepare($questionsqry);
// You only need to call bind_param once
$questionsstmt->bind_param("i",$_POST["session"]);
// get result and assign variables (prefix with db)
$questionsstmt->execute();
$questionsstmt->bind_result($dbQuestionId,$dbQuestionNo);
$questionsstmt->store_result();
$studentnum = $questionsstmt->num_rows();
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<input type="hidden" name="module" value="<?php echo $_POST['module']; ?>">
<input type="hidden" name="session" value="<?php echo $_POST['session']; ?>">
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"])
echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"])
echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
else
echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>
<?php
}
}
function StudentAnswersIsSubmitted()
{
if(!isset($_POST["answerSubmit"]))
{
return false;
}
else // All is ok
{
return true;
}
return false;
}
function StudentAnswers()
{
$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
$selectedstudentanswerstmt->bind_param($parameterTypes,implode($parameters)); //LINE 318
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute(); //LINE 327
$selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo,
$detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
$detailsMouseClick,$detailsStudentMark); //LINE 330
$selectedstudentanswerstmt->store_result(); //LINE 331
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
echo "$selectedstudentanswerqry";
}
?>
您不能發布整個文件嗎? 我看了又看,仍然找不到您問題的任何答案。 您確定$ where,$ parameters []和$ parameterTypes不會在其他地方更改嗎? 另外,我必須假定第318行是“ if(!empty($ where))”塊中的調用。
看一看fluentPDO ,Dibi fluent或一些類似的查詢生成器。 這將使您免於遭受很多苦難,他們已經解決了您要嘗試做的事情。
無論如何,而不是:
$selectedstudentanswerstmt->bind_param($parameterTypes,implode($parameters));
使用:
call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'),
array_merge(array($parameterTypes), $parameters))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.