4

Php default character set is UTF-8. All special characters in PHP and HTML are outputting as question mark like "?" in the browser. All data with special characters are stored as UTF-8 in database fields. But when PHP reads the database and output to browsers, all special characters like copyright and trademark symbols are ?

Data cannot be encoding as htmlentities, because otherwise data wouldve been outputted in the browser as html code. The fields in the database have html markups. It's like a wysiwyg field.

The other similiar question didn't really solve the problem. My problem is that php is reading a database field encoded in utf-8 including html markup, text and special characters. Then save the data in another utf-8 encoded database field. But something in the middle isn't right. After the process is done, special characters in new mysql columns are ?. Thus browser is displaying ? for all special characters.

$conn = new mysqli($host, $username, $password, $dbName);
mysql_set_charset('utf-8',$conn);


$categoryDescription = utf8_encode(utf8_decode($var['manufacturer_overview']));
kenorb
  • 155,785
  • 88
  • 678
  • 743
user2700690
  • 563
  • 3
  • 11
  • 25
  • 1
    HTML page must not be UTF8. Check the headers being sent on the request. – chris85 Mar 16 '15 at 04:33
  • 1
    possible duplicate of [UTF-8 encoded html pages show � (questions marks) instead of characters](http://stackoverflow.com/questions/5445137/utf-8-encoded-html-pages-show-%ef%bf%bd-questions-marks-instead-of-characters) – Jonathon Reinhart Mar 16 '15 at 04:33

1 Answers1

3

I had similar problem

But can be caused because of multiple reason .

1.Check if header has

<meta charset="utf-8" />

2.Open the pages that contains Special char-set only in the Editor that supports them

3.This is what happend in my case

I was saving the data in DB using php function utf8_encode() but also in the database In my execute function it was already present

mysql_set_charset('utf8',$dbSelect);

My function looks like this

public function execute($sql,$useDb = 1)
    {
        if($useDb == static::LOCAL)
        {
            $dbSelect = $this->conexion;
        }elseif ($useDb == static::REMOTE) 
        {
            $dbSelect = $this->paisConexion;
        }
        mysql_set_charset('utf8',$dbSelect);
        $result = mysql_query($sql,$dbSelect) or die("ERROR: Ejecuci&oacute;n de consulta: $sql<br>\n");

        return $result;
    }

So it was being encoded twice I removed one and now it works fine.

Hope any of these helps

Thanks

Evert
  • 93,428
  • 18
  • 118
  • 189
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • Hi, good idea but didn't work for me. see updated question. Thanks – user2700690 Mar 16 '15 at 05:13
  • @user2700690 bro remove the last ecode part and try , `utf8_encode(utf8_decode` try totally removing this and add the header most probably u would have added it already . – Vikram Anand Bhushan Mar 16 '15 at 05:16
  • thats what I had before. Believe it, if I remove encode and decode, data will be cut off at the first special character in the new output field in database. If I put encode then decode, at least I got all data in the new field but with ? mark instead of special characters... – user2700690 Mar 16 '15 at 05:20
  • This extension is deprecated as of PHP 5.5.0 http://php.net/manual/en/function.mysql-set-charset.php – user2700690 Mar 16 '15 at 05:22
  • @user2700690 Yeah I had the same problem Data was cutting off so to fix this after trying lot of things , Following method worked. while saving the data I used `addslashes()` function and before printing in any html tag I used `htmlspecialchars()` it worked . – Vikram Anand Bhushan Mar 16 '15 at 05:24
  • 1
    @user2700690 yes you are right ,about the deprecated part :) – Vikram Anand Bhushan Mar 16 '15 at 05:25
  • 1
    This is the new one. http://php.net/manual/en/mysqli.set-charset.php looks like its making process. Instead of ? alone, now its saving special characters plus  in front of it. – user2700690 Mar 16 '15 at 05:26
  • @user2700690 you will have to play around , okay keep the ecode decode function and then use the `htmlspecialchars()` also all together then try – Vikram Anand Bhushan Mar 16 '15 at 05:29
  • 1
    Got it! Its actually saving the same exact data in the new database field. I just had the browser encoding hardcoded something else. Its working now. Magical! Thanks a million! Never wouldve thought I had to force mysql encode again. – user2700690 Mar 16 '15 at 05:30