Gireesh Doddipalli
Gireesh Doddipalli

Reputation: 490

JSON working on localhost but not server?

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);

I am getting this error:

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

This works fine in localhost but returns an error on the server. What would cause this?

Upvotes: 0

Views: 279

Answers (1)

BenM
BenM

Reputation: 53198

The error message tells you what the problem is. You're using mysql_fetch_object() (which is deprecated, by the way, as is the whole mysql_* family), and then trying to access it as an array. Change your code as follows:

$row_g = mysql_fetch_object($res_g);
$res->grade = $row_g->grade;  

Upvotes: 1

Related Questions