mysql> select * from members;
+-------+-----------+-----------+
| memid | firstname | lastname |
+-------+-----------+-----------+
| 1 | billal | begueradj |
| 2 | bill | gates |
| 3 | steve | jobs |
+-------+-----------+-----------+
3 rows in set (0.00 sec)
我有这个密码:
$output = array('error' => false);
$members = array();
try {
$db = new PDO('mysql:host=localhost;dbname=bill;charset=utf8',
'root',
''
);
} catch(Exception $e) {
die('Error in connecting to DB:
'.$e->getMessage());
}
$response = $db->query('SELECT * FROM members');
while($row = $response->fetch()){
echo $row['firstname'].' ';
echo $row['lastname'].'
';
array_push($members, $row);
}
$output['members'] = $members;
$response->closeCursor();
$json = json_encode($out);
echo $json; // outputs correctly
header("Content-type: application/json"); // error here
die();
?>
当我运行包含上述PHP代码的PHP文件时,我得到了这个错误消息:
JSON数据
为什么会这样?
P、 当然,当我评论这句话时:
//header("Content-type: application/json");
错误信息消失
header("Content-type: application/json");
$output = array('error' => false);
$members = array();
try {
$db = new PDO('mysql:host=localhost;dbname=bill;charset=utf8',
'root',
''
);
} catch(Exception $e) {
die('Error in connecting to DB:
'.$e->getMessage());
}
$response = $db->query('SELECT * FROM members');
while($row = $response->fetch()){
array_push($members, $row);
}
$output['members'] = $members;
$response->closeCursor();
$json = json_encode($out);
//echo $json;
?>
仍然收到相同的错误消息