0
while($res = mysql_fetch_object($u_result)) {
  //print_r($res);
  $task_id = $res - > task_id;
  //print_r($json['task_info']);
  //$task_id = $json['task_info'][0]->task_id;
  $sql_g = "SELECT `grade` FROM `t_grades` WHERE `student_id`='$student_id' AND `task_id`= $task_id";
  $res_g = mysql_query($sql_g);

  if(mysql_num_rows($res_g) > 0) {
    $row_g = mysql_fetch_object($res_g);
    $res->grade = $row_g['grade'];
  }
  else {
    $res->grade = 'none';
  }
  //print_r($res);
  $json['task_info'][] = $res;
}
echo json_encode($json);

我收到此错误:

Fatal error: Cannot use object of type stdClass as array like this.

这在 localhost 中工作正常,但在服务器上返回错误。什么会导致这个?

4

1 回答 1

1

错误消息告诉您问题所在。您正在使用mysql_fetch_object()(顺便说一句,不推荐使用,就像整个mysql_*家庭一样),然后尝试将其作为数组访问。更改您的代码如下:

$row_g = mysql_fetch_object($res_g);
$res->grade = $row_g->grade;  
于 2013-03-09T15:12:29.593 回答