如何在mysqli中删除下面的警告

时间:2012-06-19 23:37:33

标签: php mysqli

我遇到的问题是它在mysqli中给我一个警告:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in ...on line 89

如何摆脱警告?

$questionquery = "
SELECT q.QuestionId, q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(an.Answer ORDER BY an.Answer SEPARATOR ' ') AS Answer, r.ReplyType, 
       q.QuestionMarks 
  FROM Answer an 
  INNER JOIN Question q ON q.AnswerId = an.AnswerId
  JOIN Reply r ON q.ReplyId = r.ReplyId 
  JOIN Option_Table o ON q.OptionId = o.OptionId 

                 WHERE ";

$i=0;
foreach ($terms as $each) {     
    $i++;         

    if ($i == 1){         
        $questionquery .= "q.QuestionContent LIKE ? ";     
        } else {         
            $questionquery .= "OR q.QuestionContent LIKE ? ";    
             } 
             }  

             $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY "; $i = 0; foreach ($terms as $each) {     
                 $i++;      

    if ($i != 1)         
    $questionquery .= "+";     
    $questionquery .= "IF(q.QuestionContent LIKE ? ,1,0)"; 
    } 

    $questionquery .= " DESC "; 

    $stmt=$mysqli->prepare($questionquery);      
    $stmt->bind_param('s', $each = '%' . $each . '%');
    $stmt->execute(); 
    $stmt->bind_result($dbQuestionId,$dbQuestionContent,$dbOptionType,$dbNoofAnswers,$dbAnswer,$dbReplyType,$dbQuestionMarks); 
    $questionnum = $stmt->num_rows();

输出SQL:

SELECT q.QuestionId, q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(an.Answer ORDER BY an.Answer SEPARATOR ' ') AS Answer, r.ReplyType, q.QuestionMarks 

FROM Answer an INNER JOIN Question q ON q.AnswerId = an.AnswerId JOIN Reply r ON q.ReplyId = r.ReplyId JOIN Option_Table o ON q.OptionId = o.OptionId 

WHERE q.QuestionContent LIKE ? 

GROUP BY q.QuestionId, q.SessionId 

ORDER BY IF(q.QuestionContent LIKE ? ,1,0) DESC 

1 个答案:

答案 0 :(得分:1)

你似乎把MySQLi的参数绑定方法与PDO混淆了(它接受一个数组execute()。在$each上调用bind_param()并且不将参数传递给execute()。< / p>

$stmt->bind_param('s', $each);
$stmt->execute();

请注意,如果您打算对LIKE通配符使用%个查询,那么这些通配符必须是绑定变量的一部分,这样:

$each = '%' . $each . '%';