2

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';
halfer
  • 19,824
  • 17
  • 99
  • 186
Satish Ravipati
  • 1,431
  • 7
  • 25
  • 40

2 Answers2

3

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] => 😀
)
olimortimer
  • 1,373
  • 10
  • 23
0

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;

add the following code in database.php

$db['default']['char_set'] = 'utf8mb4'; $db['default']['dbcollat'] = 'utf8mb4_unicode_ci';