json php返回字符串null

时间:2015-02-16 19:44:48

标签: php json

我正在关注一个非常有趣的教程,这对我很有帮助 但是我遇到了麻烦。

我的php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=ISO-8859-1");
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123456';
$dbname = 'directory';
$sql = "select e.id, e.firstName, e.lastName, e.title, e.picture, count(r.id) reportCount " . 
    "from employee e left join employee r on r.managerId = e.id " .
    "group by e.id order by e.lastName, e.firstName";
try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query($sql);  
    $employees = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($employees) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
?>

有些人返回空字符串,特别是当他们有重音符号时。

我尝试了几种替代方案,如

header("Content-Type: application/json; charset=utf-8");

mysql_query("SET character_set_results=utf8", $dbh) 

0 个答案:

没有答案