2013-03-09 151 views
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); 

我收到此錯誤:JSON工作在本地主機但不是服務器?

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

這個工作在本地主機正常,但在服務器上返回一個錯誤。什麼會造成這種情況?

+0

本地主機vs服務器的pvp版本是什麼? – 2013-03-09 15:10:22

+2

'$ res-> grade = $ row_g ['grade'];'應該是'$ res-> grade = $ row_g-> grade;' – BenM 2013-03-09 15:10:53

+0

其工作正常......非常感謝你 – Gireesh 2013-03-09 15:13:59

回答

1

錯誤消息告訴你問題是什麼。您正在使用mysql_fetch_object()(順便說一句,這是不推薦的,就像整個mysql_* family一樣),然後嘗試以數組的形式訪問它。更改您的代碼,如下所示:

$row_g = mysql_fetch_object($res_g); 
$res->grade = $row_g->grade; 
相關問題