简体   繁体   中英

CodeIgniter Active Record removing special characters

I'm new to CodeIgniter and have come into an issue with the insertion of special characters into the MySQL database. I am inserting data into the db using this code:

return $this->db->insert('table_name', $data);

When I view the text in the db, all '€' signs are changed to '?' signs. Also, there was a sentence with 'á' in it and it was removed, and all text after it was not inserted into the db.

I did a var_dump on $data just before the insert and the characters are correct.

The MySQL collation for the field is latin1_swedish_ci . I changed it to utf8_general_ci to see if it would make a different but then the '€' sign was completely omitted.


SOLUTION : Before the db insert, set the charset to latin1 :

$this->db->query("SET NAMES 'latin1'");
return $this->db->insert('table_name', $data);

Another solution was to change the CodeIgniter database file ( config/database.php ). The code should read:

$db['default']['char_set'] = 'latin1';
$db['default']['dbcollat'] = 'latin1_swedish_ci';

What is your charset configured to in the config/database.php file? See here for info on database config file.

EDIT:

Also look at this definitive guide to UTF-8 and CI .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM