2014-09-20 52 views
0

我試圖與PDO一些數據展示給我的broswer但它顯示了像錯誤?????????? 在輸出中。 我試着把$db -> exec('set names utf8');但它改變牛逼PDO輸出UTF8插入

Ø039c \ u0395 \ u039d \ u039f \ u0399 \ u03a0 \ u03a5 \ u03a1 \ü每

。如果我把英語內容到我的數據庫一切工作和輸出確定,但如果我把希臘的問題來了,我需要希臘插入

這裏是一個不折不扣的把沒有$ DB - > EXEC(「設置名稱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; 

?> 
Any help? 

編輯。解決的辦法是隻添加JSON_UNESCAPED_UNICODE

echo json_encode($response,JSON_UNESCAPED_UNICODE); 
+0

你可以用'函數utf8_encode() '因爲jsonencode只能使用該編碼 – meda 2014-09-20 22:18:47

回答

1

由於要連接到MySQL數據庫,你可以採取以下屬性

$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 

的優勢,並刪除exec(set ..)