我正在尝试使用PDO向浏览器显示一些数据,但它在输出中显示错误,如 ?????????? 。我试着放$db -> exec('set names utf8')
;但它改为:
o 039c \ u0395 \ u039d \ u039f \ u0399 \ u03a0 \ u03a5 \ u03a1 \ u every
如果我把英文内容放到我的数据库中,一切正常,输出没问题但是如果我把希腊语问题解决了,我需要希腊插页
以下是没有$db -> exec('set names utf8')
的输出:
"eventID":"3",
"catID":"1",
"poiID":"1",
"eventSourceURL":"??????? ??? ??????? ",
"eventTitle":"??????? ??? ??????? ",
"eventDescr":null,
"eventDate":"",
"eventImage":null,
"eventTime":null,
"eventComments":null,
"eventPreDescription":null
}
这是我的PDO
<?php
require('db_params.php');
$response = array();
try {
$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass);
$db -> exec('set names utf8');
$query = "select * from events where catID=1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
if ($stmt->rowCount() > 0) {
$response["success"] = true;
$response["message"] = "Post Available!";
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){
$response["posts"]["pid"] = $row;
}
}else{
$response["success"] = false;
$response["message"] = "No Post Available!";
}
}
catch (PDOException $ex) {
$response["success"] = false;
$response["message"] = "Database Error!";
}
echo json_encode($response);
//$statement->closeCursor();
//$pdoObject = null;
?>
解决方案是添加JSON_UNESCAPED_UNICODE:
echo json_encode($response,JSON_UNESCAPED_UNICODE);
答案 0 :(得分:1)
由于您要连接到mysql数据库,因此可以利用以下属性
$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
然后移除exec(set ..)