0

While trying to figure out possible reason for the error, the reason seems to be encoding some where in my web app. The strings look fine when I checked them by using var_dump but upon insertion into the database, I see this characters in my table. I am using PDO for executing SQL queries.

var_dump($string); // the string looks fine here ex- Shopaholics’

// function to create db connection
    function db_connect() {
            if(self::$db_con === null) {
                $db = self::$databases[self::$use_db];
                self::$db_con = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']);
                self::$db_con->set_charset('utf8');
            }
            return self::$db_con;
    }

   // code to insert into Db
   $con = db_connect();
   $stmt = $con->prepare(INSERT INTO case_study (case_id, description_case)
           VALUES (NULL, :description));
   $stmt->bindParam(:description, $description);
   $stmt->execute();
halfer
  • 19,824
  • 17
  • 99
  • 186
mask man
  • 351
  • 2
  • 11
  • Is `$description` also UTF-8 encoded? – RichardBernards Nov 26 '14 at 12:56
  • @RichardBernards it is just a string. – mask man Nov 26 '14 at 12:57
  • What character encoding and collation did you set on the `description_case` column when creating the `case_study` table? What is the default encoding for the table, the database? – Bobulous Nov 26 '14 at 13:00
  • @Arkanon Collation - utf8_bin – mask man Nov 26 '14 at 13:01
  • Try setting it to utf8_unicode... And all strings in PHP are also encoded in a certain way - check http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – RichardBernards Nov 26 '14 at 13:02
  • @RichardBernards string is also encoded with utf-8, just checked. i tried setting to uttf8_unicode but no help – mask man Nov 26 '14 at 13:07
  • Where does `$description` come from? Form input field? – RichardBernards Nov 26 '14 at 13:09
  • yes from the front end and meta tag encoding also set to utf8 – mask man Nov 26 '14 at 13:10
  • 1
    I'm pretty sure changing the database encoding now won't change the strings back to what they should be. It will allow you to store the characters in the future, but you'll have to go through and manually change the existing characters to what they should have been. – hukir Nov 26 '14 at 13:15
  • You need to instruct the browser to use **UTF-8**, the database **utf8** (note the hyphen). See the duplicate for all the steps. – deceze Nov 26 '14 at 13:15

0 Answers0