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 に答える