Pdo+mysql and utf-8 encoding problem;


i use pdo library with database , but i have problem with inserting any data encoded with utf-8 , like ‘arabic’ words , it’s inserted into the database but it inserted as ‘???’ chars !!

in my own framewrok . after i defined the pdo connection , i sent 2 queries to set names&charset to utf-8

example :

loadclass("PDO",array(          "mysql:host=".confitem('database','host').";port=".confitem('database','port').";dbname=".confitem('database','name') ,          confitem('database','username') ,          confitem('database','password') ,          array("PDO::ATTR_PERSISTENT" => confitem('database','pconnect'))         )      );

 $this->query('SET NAMES '.confitem('database','charset'));

 $this->query('SET CHARACTER SET '.confitem('database','charset')); 

note : i solved this problem by using function jsons_encode to convert data before inserting it to the database , and using json_decode to decode it after fetching ,.

so please help me to solve this problem !, any suggestions ??

What is the character set on the table you’re inserting into?

That should also be UTF-8.

Also, it may be that your data is stored safely in the database, but being displayed incorrectly after you retrieve it. Where are you seeing the ‘???’. In PHPMyAdmin? MySQL Query Browser?

hello Immerse

yes tables are utf-8

i see ‘???’ in site browser and PHPMyadmin :blush:

Are the pages in your site being sent with the correct character encoding?

yes :frowning: , my pages charset is UTF-8

Hmm, I’m out of ideas for now. Maybe someone else has a solution!


  1. HTML page content-type utf-8 (even pages that are PHP only)
  2. Tables’ fields in database collation utf-8
  3. Database collation set to utf-8
  4. Do a “SET NAMES UTF8” after database connection (note: no hyphen)

That’s all there is to it.

hello ,

yes i check all points and already its ! .

note : after database connection i sent 2 queries :

			$this->query('SET NAMES '.confitem('database','charset'));
			$this->query('SET CHARACTER SET '.confitem('database','charset'));

where confitem(‘database’,‘charset’) result is : UTF8

!!! ?:frowning:

Try setting the connection to utf-8 when is making it’s connection to the database.

$db = new PDO('dblib:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);