Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter not accepting "utf8mb4" as default charset

Good day,

I am using Codeigniter to develop my webapp. PHP version on the server is 5.6.22 and Mysql version of the db is 5.6.27. In codeigniter's database.php when I change the charset to "utf8mb4" from "utf8", I get the following message .(image attached)

Unable to set client connection character set: utf8mb4

enter image description here

UPDATE I can run the codeigniter app on another server that has the same php version and the same mysql version. So it must be something pertaining to httpd.conf or php.ini, maybe?

like image 707
Waris Ali Avatar asked Jul 14 '16 12:07

Waris Ali


People also ask

How do I change utf8 to utf8mb4?

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; For each column: ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; (Don't blindly copy-paste this!

How to enable UTF-8 in PHP?

PHP UTF-8 Encoding – modifications to your php. The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

What is the difference between utf8mb4 and utf8 charsets in MySQL?

utf-8 can store only 1, 2 or 3 bytes characters, while utf8mb4 can store 4 bytes characters as well. utf-8 is a subset of characters given by utf8mb4 .


1 Answers

In database.php

Set char_set to utf8mb4 and dbcollat to utf8mb4_unicode_ci or utf8_general_ci


I tested this with my Codeigniter 3.0

$db['default'] = array(
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8mb4',
    'dbcollat' => 'utf8mb4_unicode_ci',

);

And in controller I add this

$CI = &get_instance();
$CI->load->database();
echo $CI->db->char_set;
echo "<br>";
echo $CI->db->dbcollat;

Output

utf8mb4
utf8mb4_unicode_ci

Read this

  1. Using utf8mb4 with php and mysql
like image 60
Abdulla Nilam Avatar answered Oct 17 '22 06:10

Abdulla Nilam