Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert emoticons into MySQL using CodeIgniter

How to insert unicode characters into MySQL and retrieve them using CodeIgniter? Is there any settings to be done.

This is the query when I print it.

INSERT INTO `reviews` (`description`) VALUES ('😀')

But, it is saving as ????.

Here is what I have done. CodeIgniter database.php

$db['default']['char_set'] = 'utf8mb4';
$db['default']['dbcollat'] = 'utf8mb4_unicode_ci';
like image 402
Satish Ravipati Avatar asked Sep 06 '26 00:09

Satish Ravipati


1 Answers

It works fine for me when I test it (editor is Sublime Text).

Create MySQL table:

CREATE TABLE `reviews` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Run an insert in CI:

$this->db->insert('reviews', array('description' => '😀'));

Grab our data back:

$result = $this->db->get('reviews');

Output our result:

echo '<pre>';
print_r($result->row());
echo '</pre>';

Our result:

stdClass Object
(
    [id] => 1
    [description] => 😀
)
like image 170
olimortimer Avatar answered Sep 08 '26 18:09

olimortimer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!