【发布时间】:2015-09-24 10:53:55
【问题描述】:
我从 MySQL 获取数据并使用 php 以 json 格式显示,但我的数据是波斯语,它显示 ? 字符而不是原始数据
如下所示。
{"allnews":[{"id":"35","Onvan":"?????? ?????? ????? ????? ?????? ?? ?? ?????"},{"id":"36","Onvan":"?????????? ??? ? ????????????? ?????"},{"id":"37","Onvan":" ??????? ??? ??? ??? ??? ??? ?? ??? ?? ??? ??? ?????? ??? ??? ?????? ?? "},{"id":"38","Onvan":" ????? ?????? ??? ??? ?? ?? ?????? ????????"},{"id":"39","Onvan":"??? ??? ??? ????"}]}
这是我的代码。你能检查一下哪里出错了。
<?php
$hostname='localhost';
$username='xxxxxxxxx';
$password='xxxxxxxxx';
$response = array();
try {
$dbh=new PDO("mysql:host=$hostname;dbname=dbtest",$username ,$password);
$response["allnews"] = array();
/*** QUERY ****/
$sql='SELECT * FROM test';
$stmt=$dbh->query($sql);
$objs = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach($objs as $object) {
$news = array();
$news["id"]=$object->id;
$news["Onvan"]=$object->title;
array_push($response["allnews"], $news);
}
echo json_encode($response);
/*** close connection ***/
$dbh=null;
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
【问题讨论】:
-
我要问,你确定它们在数据库中存储正确吗?