JSON在localhost上工作但不在服务器上工作?

时间:2013-03-09 15:08:50

标签: php json

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中工作正常,但在服务器上返回错误。会导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:1)

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

$row_g = mysql_fetch_object($res_g);
$res->grade = $row_g->grade;  
相关问题