0

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:

4
  • Why are you using htmlentities() for something that's being stored in your database? That function is for escaping HTML, you use it on your output. Commented Jan 15, 2012 at 15:43
  • @Madmartigan Because if don't use this, I face these problems stackoverflow.com/questions/8864127/… and stackoverflow.com/questions/8860407/… Commented Jan 15, 2012 at 15:46
  • @Madmartigan Please read my problems and then answer me Commented Jan 15, 2012 at 15:48
  • I don't have an answer, which is why I didn't post one, but I wanted to comment about the side issue. There's no actual reason to encode to HTML entities in this context. Commented Jan 15, 2012 at 15:53

2 Answers 2

4

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.

  • If you received the data from a form, add accept-encoding="utf-8" to the form element.
  • Verify that your input string could be a valid utf-8 stream with TRUE===mb_check_encoding($string, 'UTF-8')
  • Check that the byte sequence is as expected by counting characters: mb_strlen($string, 'UTF-8') should return the same number of characters as what you see, and a number less than strlen($string) (which counts bytes).

In your application/config/database.php, ensure you have these two settings for your database connection:

$db[$dbgroupname]['char_set'] = "utf8";
$db[$dbgroupname]['dbcollat'] = "utf8_unicode_ci";

Replace $dbgroupname with the group name of your connection (e.g., 'default').

Don't use htmlspecialchars or htmlentities on 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-Type header includes charset=utf8 and your html document's head looks like this:

<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I counts the characters. There is a difference b/w mb_strlen($string, 'UTF-8')=29 and strlen($string)=31. String is "HTML5′s placeholder Attribute"
If i don't use the htmlentities(). My string becomes HTML5â?²s placeholder Attribute
Yes, but where does it become that? The very thing you are using to view the string may be corrupting it!
But it save in database with the same view like "HTML5â?²s placeholder Attribute"? What should i do now? i think problem is (′).
Please follow the steps on the "On Output" section of my answer. What you are using to view the string in the database may itself not understand "UTF-8". In other words, it may be stored in the database correctly, only displaying incorrectly.
|
0

Try this:
Add this mysql query in the system/database/DB.php file at the end of DB function. (before return $DB; ) .

$DB->simple_query('SET NAMES \'utf8\'');

3 Comments

I try the query but getting same problem.
problem is mentioned in my question. and in two questions stackoverflow.com/questions/8864127/… and stackoverflow.com/questions/8860407/…
This is unnecessary. Codeigniter does this for you in mysql_driver.php, db_set_charset() method, which is run as part of connection initialization.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.