Home » Php » Emoji from Angularjs $http to PHP json_decode and store in mySQL

Emoji from Angularjs $http to PHP json_decode and store in mySQL

Posted by: admin October 29, 2018 Leave a comment


So i am trying to store emojis in my database and return them in my app. I have build a small messanger with ionic v1 and everything works fine. I only want to store the emojis properly and show them in app. My whole database, tables and columns are set to utf8mb4 so I know this is good. When i send it with $http to my php it also looks okey : {"from_account_id":"xxxx","for_account_id":"xxxx","message":"?"}: But when I print it in php it looks like this : {"from_account_id":"106191","for_account_id":"989014","message":"đ˝"} and in my mysql everywhere I have put an emoji it shows ?. What more can I do ? I also have set the header in my php to UTF_8. I know there were much question about that but i have followed most of them and It still didn’t resolve my problem.

My php where i store the message :

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']);
  } catch (PDOException $e) {
          throw $e;
  echo 'Success';


My AngularJS function where I send the message :

  $scope.sendMessage = function(for_account_id, message){
    if(for_account_id != null && message != null){
      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;
      $rootScope.webSocket.send($stateParams.chatId + "|" + $rootScope.unique_id  + "|" + 'newMessage');

And this is my connection with database :


$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');
} catch (PDOException $e) {
      echo $e->getMessage();

Any tips or idea what I miss?