Im trying to display some Indian language characters (Hindi, Tamil, etc) in a php file, taking the data from a MYSQL database.
It works fine with mysqli_query
, but with PDO, the characters show up as garbage values. I am not able to figure out why, as I have set every possible aspect to work with utf-8
.
Here is my connection string : (I am using PHP version 5.6.8)
$db_connection = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8;', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
After doing some research I discovered the utf8_decode
function, and decided to try that. It works only partially, however.
These are the results (as labeled)
PDO result without utf8 decoding:
आज सदीओ के बाद खà¥à¤¦à¤•ो हम संà¤à¤¾à¤²à¥‡ हà¥à¤...
PDO result with utf8 decoding:
��?��? सद��?��? ��?��? बाद ��?ुद��?��? हम स��?भाल��? हुए...
MYSQLI_QUERY:
आज सदीओ के बाद खुदको हम संभाले हुए...
Since only PDO is having this problem, I'm assuming my DB and headers are fine. Please let me know if any more code is needed (Queries are standard DB calls).
EDIT: I have done the following steps already, with regards to utf-8
settings :
- Setting DSN -
charset=utf8;
- Adding it to PDO's options array -
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
- Setting it via PDO object's
exec
function :
$db_connection->exec("SET NAMES utf8");
$db_connection->exec("SET CHARACTER SET utf8");