0

I have a problem with my code. I created a php page which make a connection to a database, extract some fields from a table then echo the result of the query to a html div in the body. The problem is my website is in croatian, so i have characters like č,ž,š... which are not displayed (it display a question mark instead). I searched the web for like 2 days without a solution. I set a

header('Content-Type: text/html; charset=utf-8');

I also set html meta tag:

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

my php page is saved in utf-8 with unicode normalization form C, the database have charset UTF-8, the table and fields are all utf-8, the collation of the database is utf-8 general ci.

If you need I can provide my code too (I just avoid it at the moment to don't make the post too long).

Another interesting thing is that if I create in my php file a normal html tag like a and write something in it with č,ć,š the characters are displayed but when I use php to extract some info from the base then the characters are replaced. Also in the database the characters are all correct and everything looks good. Last thing, my page loads k2 items from a k2 page in an iframe, and all the rest of the pages work fine.

I really don/t know how to fix that so every suggestion will be well accepted :D

Aleksander
  • 101
  • 11
  • 1
    possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – chris85 Aug 12 '15 at 11:57
  • This question will probably be closed as a duplicate soon. If the answers from the duplicates don't fully address your question please [edit] it to explain what makes it unique, then flag it for re-opening. Thanks! – Mogsdad Aug 12 '15 at 20:05
  • Not enough info. "the characters are replaced" -- what with? Perhaps Mojibake `Ä,ć,Å¡`? – Rick James Aug 28 '15 at 05:00

3 Answers3

0

Probably you forget about setting connection charset to UTF-8

You need do SQL command set names 'utf8'

Check this

Robert
  • 19,800
  • 5
  • 55
  • 85
0

If it's only characters extracted from your database, and they are stored correctly in the database, it might be with your connecting to your database.

Try running this code directly after connecting to your database:

  • $conn->set_charset("utf8"); for MySQLi (OOP)
  • mysqli_set_charset("utf8"); for MySQLi (Procedural)
  • mysql_set_charset("utf8"); for MySQL (deprecated)

Or for PDO, it's done when making the object

  • $dbh = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass); for PDO
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

Try Using this...in you config file ........

$mysqli = new mysqli("localhost","root","password","db");

$mysqli->query("SET NAMES 'utf8'");

$mysqli->query("SET CHARACTER SET utf8");

may be this will help...

Sahil Manchal
  • 472
  • 6
  • 20