关于非对象和未定义变量的注意事项

时间:2013-01-09 17:12:06

标签: php mysqli

我试图通过在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变量:

  

注意:未定义的变量:行...在线...

     

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题