How to Enable Emoji on MySQL with PHP

This is a short tutorial on how to enable Emojis with MySQL and PHP. Simple 5 Steps to get started with Emojis MySQL.

I struggled so much getting iOS and Android Emoji’s to work with my Cordova Application. The Server Side of my application is in PHP and Runs a MySQL Database. 

After reading and trying i finally figured out the steps to get your Emoji running. 

1) Change your DB Character Set and Collation:

SET NAMES utf8mb4;
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

2) Change your Tables Character Set and Collation: 

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

3) Change Column Character Set and Collation: 

ALTER TABLE table_name CHANGE column_name column_name VARCHAR(140) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;

4) Make Sure you PHP Connection with MySQL is also on Same Character Set and Collation

5) Use the Following Code to Insert into Database and to Retrieve.

//Insert Code
$data_insert = Array (
  "message" => json_encode($message),
  "revision_id" => $revision_id,
  "user_id" => $user_id,
  "date_created" => date('Y-m-d H:i:s'),
);

$id = $db_2->insert ('chats', $data_insert);

//Retrieve Code
$result_json = json_decode($record['message']);

if (json_last_error() === JSON_ERROR_NONE) {
    $return_message = json_decode($record['message']);
} else {
	$return_message = $record['message'];
}



$data[] =  array(
	'chat_id' => $record['chat_id'], 
	'type' => $type, 
	'message' => $return_message, 							
	'full_name' => $user_info['full_name'], 
	'email' => $user_info['email'], 
	'profile_image' => json_decode($user_info['profile_image']), 
	'date_created' => $record['date_created'],
  	'date_created_nice' => time_elapsed_string(strtotime($record['date_created'])), 
);