I am creating an application using CodeIgniter/MySQL. The problem is that when I store a text with Quotes or Apostrophe. It stores an Blank value to MySQL database.
function add_article($title, $desc){
$data = array(
'title' => htmlentities($title, ENT_QUOTES, "UTF-8"),
'desc' => htmlentities($desc, ENT_QUOTES, "UTF-8")
);
if($this->db->insert('articles', $data)){
return TRUE;
} else {
return FALSE;
}
}
The character set of my table “articles” is “utf8” and COLLATE is utf8_unicode_ci. And HTML character set is also UTF-8. what’s wrong? Please help me, and thanks in advance. If i don’t use htmlentities() function i face two problems which i mention in my comments below:
You could have problems either on input or on output.
On Input
First of all, make sure that the string you receive actually is utf-8.
accept-encoding="utf-8"to theformelement.TRUE===mb_check_encoding($string, 'UTF-8')mb_strlen($string, 'UTF-8')should return the same number of characters as what you see, and a number less thanstrlen($string)(which counts bytes).In your
application/config/database.php, ensure you have these two settings for your database connection:Replace
$dbgroupnamewith the group name of your connection (e.g.,'default').Don’t use
htmlspecialcharsorhtmlentitieson data before you store it. Use those on output, in your views.On Output
Ensure that whatever you are using to view the data that comes out of your database expects a utf-8 encoding.
For html, make sure your
Content-Typeheader includescharset=utf8and your html document’sheadlooks like this:Check your results in multiple browsers. Some browsers do charset sniffing and might choose a different charset than what you declare. If so, this means that something on your page is not valid UTF-8–find that thing and eliminate.
If you are using some kind of database viewer (PHPMyAdmin, Navicat, etc), make sure the connection is configured to expect utf-8.