emoji从angularjs$http到php json\u解码并存储在mysql中

e0bqpujr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(325)

所以我尝试在我的数据库中存储emojis并在我的应用程序中返回它们。我已经建立了一个小messanger与离子v1和一切工作正常。我只想储存的表情正确,并显示在应用程序中。我的整个数据库、表和列都设置为utf8mb4,所以我知道这很好。当我把它和 $http 给我的 php 看起来也不错: {"from_account_id":"x","for_account_id":"x","message":"?"}: 但是当我把它打印出来 php 看起来是这样的: {"from_account_id":"106191","for_account_id":"989014","message":"đ˝"} 在我的mysql里,我到处都放了一个表情符号 ? . 我还能做什么?我还将php中的header设置为 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();
    }
?>

你知道我错过了什么吗?

zlhcx6iw

zlhcx6iw1#

好的,我已经解决了我的问题。我做这件事的方式也许不是最好的,但它是有效的。所以基本上,在数据库中存储Unicode就这么简单:
我换了衣服 header 在插入数据的文件中: header('Content-Type: application/json; charset=utf-8'); 我再次将消息编码为json,同时插入如下内容: $stmt->bindValue(':message', json_encode($data['message'])); 再次响应我的应用程序之前,请将标题设置为: header('Content-Type: application/json; charset=utf-8'); 我的代码如下所示:

<?php
header('Content-Type: application/json; 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', json_encode($data['message']));
    $stmt->execute();
  } catch (PDOException $e) {
    header('Content-Type: text/html; charset=utf-8');
          throw $e;
  }
  header('Content-Type: text/html; charset=utf-8');
  echo 'Success';

?>

所以这个问题被解决了,Unicode存储在我的数据库中。
然后我又陷入了一个尴尬的境地,就像从 mysql 并将其编码为 json 在发送到我的应用程序时,我的应用程序无法将Unicode返回到特殊字符,即使我正在绑定它 ng-bind-html 以及使用 $sce.trustAsHtml() . 通常我只是把我的查询结果作为 encoded json 但它没有像我说的那样起作用。所以我做的是这样的:

<?php
header('Content-Type: application/json; 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("SELECT * from messages WHERE (from_account_id = :unique_id AND for_account_id = :user_2) OR (from_account_id = :user_2 AND for_account_id = :unique_id)");
  $stmt->bindValue(':unique_id', $data['user_1']);
  $stmt->bindValue(':user_2', $data['user_2']);
  $stmt->execute();
  } catch (PDOException $e) {
          throw $e;
      }

  $rows = $stmt->rowCount();
  $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

  if ($rows > 0){
    $i = 0;
    echo "[";
    foreach ($result as $key => $value){
      $i++;
      echo "{";
      echo '"message_id"' . ":" . '"' .$value['message_id']. '"' .", ";
      echo '"from_account_id"' . ":" . '"' .$value['from_account_id']. '"' .", ";
      echo '"for_account_id"' . ":". '"' .$value['for_account_id']. '"' .", ";
      echo '"date_sent"' . ":" . '"' .$value['date_sent']. '"' .", ";
      echo '"date_read"' . ":" . '"' .$value['date_read']. '"' .", ";
      echo '"message"' . ":" . '"' .str_replace("\n", "</br>", json_decode($value['message'])). '"' ;
      echo "}";
      if($i != $rows){
        echo ",";
      }
      if($i == $rows){
        echo "]";
      }
      echo "\n";
    }
    exit;
  }
  else {
    echo 'Something went wrong';
    exit;
  }

?>

发送已解码 json 在一个字符串中,我的angularjs作为我自己制作的真正的json得到了解决方案。
我在这个问题上加了一个答案,因为也许有人会遇到同样的麻烦,这会很有帮助。

相关问题