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

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?

3
  • what's the pvp version of your localhost vs server? Commented Mar 9, 2013 at 15:10
  • 2
    $res->grade = $row_g['grade']; should be $res->grade = $row_g->grade; Commented Mar 9, 2013 at 15:10
  • I have added it as an answer, you might like to accept ;) Commented Mar 9, 2013 at 15:14

1 Answer 1

1

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;  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.