CodeIgniter Forums
Diacritical marks are truncated via Active Record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Diacritical marks are truncated via Active Record (/showthread.php?tid=349)



Diacritical marks are truncated via Active Record - marksalvatore - 11-24-2014

A string containing a diacritical mark (ex, "Erlösten"), is getting truncated to "Erl" when stored in the database via Active Record.

     $values['name'] = "Erlösten";
     $this->db->insert($this->_table, $values);

will store the 'name' value as "Erl".
The 'name' field in the database is set as varchar(100) with encoding set to  'UTF-8 Unicode'.

Is there a configuration setting I've overlooked?


RE: Diacritical marks are truncated via Active Record - marksalvatore - 11-24-2014

If I don't use active record to insert the data, the full string is stored properly, so that suggests the problem might be in how active record is inserting the data. Could this be a bug in CI's implementation of active record? or maybe  just a configuration adjustment on my side?


RE: Diacritical marks are truncated via Active Record - Rufnex - 11-24-2014

This must be a charset problem. Have you checked that your database connection is utf8, that your files (controller, views, etc.) are saved as uft8? And in your /application/config/database.php should be also the utf8 :

Code:
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',

And your database should have this encoding: utf8_general_ci


RE: Diacritical marks are truncated via Active Record - marksalvatore - 11-24-2014

I agree, that this has got to be a charset problem, however all is already set to utf8. The database config file contains those exact values. The encoding for the database field is utf8 with collation set to "utf8_general_c"i. And the default file encoding in my IDE is, and has always been set to utf8.

What about the database driver? Wouldn't that need to be utf8 as well? Any idea how to check that?


RE: Diacritical marks are truncated via Active Record - Rufnex - 11-24-2014

Hm strange, do you connect with mysql, mysqli or pdo?


RE: Diacritical marks are truncated via Active Record - marksalvatore - 11-25-2014

I connect with mysqli.


RE: Diacritical marks are truncated via Active Record - marksalvatore - 11-25-2014

So the diacritical marks truncate the 'name' string when using this db call:

$values['name'] = "Erlösten";
$this->db->insert($this->_table, $values);

And with this db call:

$sql = "UPDATE tblNames SET name='Erlösten' WHERE key = '$key'";
$this->db->query($sql);

But not when I use this db call:

$sql = "UPDATE tblNames SET name='Erlösten' WHERE key = '$key'";
$conn = new mysqli($hostname, $username, $password, $database);
$conn->query($sql);

Any ideas why?
Whether it's UPDATE or INSERT doesn't make a difference. The same result is produced by both in all three cases.


RE: Diacritical marks are truncated via Active Record - Rufnex - 11-26-2014

Hm still strange. Have double checked your database if it has the correct encoding. On some servers i have seen, that if you create a database with utf8 its been created with latin1_swedish_ci. For that you have to turn it back to utf8 .. so not only the fields of a table should have utf8 also the database itself.

Finaly you can the this config without dbcollat

PHP Code:
'char_set' => 'utf8',
    
'dbcollat' => ''



RE: Diacritical marks are truncated via Active Record - marksalvatore - 11-26-2014

Unfortunately, I verified that my database is also set to encoding = "utf8". And I tried your suggestion of 'dbcollat' => '', but that didn't make a difference either. Could this be a legitimate bug?


RE: Diacritical marks are truncated via Active Record - Rufnex - 11-26-2014

I think this should be not a bug. But we should figure this out. Can you tell us about your environment (server os, db version, php version, etc.) .. and CI 2 or 3?