0

我在下面有一个代码示例,我正在尝试使用 call_user_func_array();

        function make_values_referenced (&$arr) { 
            $refs = array(); 
            foreach ($arr as $key => $value) {
              $refs[$key] = &$arr[$key];
            }
            return $refs;
          }

        $selectedstudentanswerqry = "
        SELECT q.SessionId, QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, 
        GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType
        FROM 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);

                    // Make the referenced array
            $referencedArray = make_values_referenced(array_merge(
             (array($parameterTypes, $parameters))));

            // You only need to call bind_param once
            call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'),make_values_referenced($referencedArray)); //LINE 331

    }

    $selectedstudentanswerqry .= "
      GROUP BY sa.StudentId, q.QuestionId
      ORDER BY StudentAlias, q.SessionId, QuestionNo
    ";

    // get result and assign variables (prefix with db)
    $selectedstudentanswerstmt->execute();  //LINE 341
    $selectedstudentanswerstmt->bind_result($detailsSessionId,$detailsQuestionNo, 
$detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType); //LINE 344 

$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows(); 
$selectedstudentanswerstmt->close();

现在我尝试在这里使用手册来指导我:http ://ca.php.net/manual/en/mysqli-stmt.bind-param.php#96770

我遇到的问题是我收到了很多错误,我的问题是我需要帮助纠正上面的代码才能解决这个问题。

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in app/...  on line 331 

Warning: mysqli_stmt::execute(): (HY000/2031): No data supplied for parameters in prepared statement in /app/... on line 341 

Warning: mysqli_stmt::bind_result(): (HY000/2031): No data supplied for parameters in prepared statement in app/...  on line 344


 Warning: mysqli_stmt::store_result(): (HY000/2014): Commands out of sync; you can't run this command now in app/... on line 348
4

1 回答 1

1

这是一种通用的方法,可能有点不好的做法,但这可以完成工作,了解哪个条件并添加绑定参数和参数类型,具体取决于选择的选项:

if (count($where) == 1) {
  $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) == 2) {
  $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) == 3) {
   $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
}
于 2013-02-05T01:04:06.863 回答