0

我试图通过在 PHP 中构建错误答案列表来检索每个问题的错误答案。例如,通过包含原始查询中的单行,来计算错误答案,如下所示:

 $query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, an.Answer, an.AnswerId 
   FROM Session s  
   INNER JOIN Question q ON s.SessionId = q.SessionId 
   JOIN Answer an ON q.QuestionId = an.QuestionId 
   WHERE s.SessionName = ? 
   ORDER BY q.QuestionId, an.Answer 
   "; 
       
   // prepare query 
   $stmt=$mysqli->prepare($query); 
   // You only need to call bind_param once 
   $stmt->bind_param("s", $assessment); 
   // execute query 
   $stmt->execute();  
   
   $specialOptionTypes = array( 
    'Yes or No' => array( 'Y', 'N' ), 
    'True or False' => array( 'T', 'F' ), 
); 

// Do this for each row: 
if ( array_key_exists( $row->OptionType, $specialOptionTypes ) ) { 
    $options = $specialOptionTypes[ $row->OptionType ]; 
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $row->OptionType, $match ) ) { 
    $options = range( $match[1], $match[2] ); 
} else { 
    // issue warning about unrecognized option type 
    $options = array(); 
} 
$right = str_split( $row->Answer );  // or explode() on a delimiter, if any 
$wrong = array_diff( $options, $right ); 



    // Fetch the results into an array 

   // get result and assign variables (prefix with db) 
   $stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbAnswer, $dbAnswerId); 
      while ($stmt->fetch()) { 
         
      $incorrect_ans[] = $wrong; 

      }     
       
       
      .... 
       
      //table row 
       
      <td class="answertd" name="incorrectanswers[]"><?php
echo $incorrect_ans[$key];
?></td> 

我遇到的问题是我没有得到错误的答案,而是收到了几个错误:

第一个错误是针对$row上面代码中显示的任何变量,其中指出:

注意:未定义的变量:row in ... on line ...

注意:试图在线获取...中非对象的属性...

第二个问题在上面的表格行中,而不是显示不正确的答案,而是显示Array

我的问题是如何修复通知和数组以至少有一个无错误的应用程序?

4

1 回答 1

0

如果你没有初始化$row到 if 语句之外的东西,那么它是未定义的。

这意味着$row不是一个对象,在某些情况下它可能是 NULL 或 false(没有找到),因为它不可访问。超出范围

于 2013-01-09T17:15:45.670 回答