【发布时间】:2018-05-25 14:37:50
【问题描述】:
所以我试图将表情符号存储在我的数据库中并将它们返回到我的应用程序中。我用 ionic v1 构建了一个小信使,一切正常。我只想正确存储表情符号并在应用程序中显示它们。我的整个数据库、表和列都设置为 utf8mb4,所以我知道这很好。当我用$http 将它发送到我的php 时,它看起来也不错:{"from_account_id":"xxxx","for_account_id":"xxxx","message":"????"}: 但是当我在php 中打印它时,它看起来像这样:{"from_account_id":"106191","for_account_id":"989014","message":"đ˝"} 并且在我的mysql 中到处我都放了一个表情符号,它显示?。我还能做什么?我还将我的 php 中的标题设置为UTF_8。我知道对此有很多问题,但我已经关注了大多数问题,但它仍然没有解决我的问题。
我存储消息的 php :
<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
include_once 'config.php';
$data = file_get_contents("php://input");
$data = json_decode($data, true);
try {
$stmt = $dbh->prepare("INSERT INTO `messages` (`message_id`, `from_account_id`, `for_account_id`, `date_read`, `message`)
VALUES (NULL, :from_account_id, :for_account_id, NULL, :message)");
$stmt->bindValue(':from_account_id', $data['from_account_id']);
$stmt->bindValue(':for_account_id', $data['for_account_id']);
$stmt->bindValue(':message', $data['message']);
$stmt->execute();
} catch (PDOException $e) {
throw $e;
}
echo 'Success';
?>
我发送消息的AngularJS 函数:
$scope.sendMessage = function(for_account_id, message){
if(for_account_id != null && message != null){
$http({
method: 'post',
url: 'myurl',
data: {
from_account_id: $rootScope.unique_id,
for_account_id: for_account_id,
message: message,
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(data){
$scope.thisMessage = null;
$scope.getMessages();
$rootScope.webSocket.send($stateParams.chatId + "|" + $rootScope.unique_id + "|" + 'newMessage');
$scope.markAsRead();
})
}
}
这是我与数据库的连接:
<?php
$hostname='myhost';
$username='myuser';
$password='mypassword';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
try {
$dbh = new PDO("mysql:host=$hostname;dbname=mydbname;charset=utf8mb4",$username,$password, $options);
$dbh->query ('SET NAMES utf8');
$dbh->query ('SET CHARACTER_SET utf8_unicode_ci');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
任何提示或想法我错过了什么?
【问题讨论】: