27

Possible Duplicate:
Save Data in Arabic in MySQL database

I have a problem with retrieving Arabic data from MYSQL database using PHP, it appears as question marks "????" in HTML:

  1. I have a database with "utf8_general_ci" as collation.
  2. The database contains some data in Arabic Language.
  3. The HTML encoding is "UTF-8".
  4. When I tried to retrieve the data in HTML, it appears as "?????".

Please Help !!!

Community
  • 1
  • 1
CairoCoder
  • 3,091
  • 11
  • 46
  • 68
  • 5
    After you connect to the database, execute this query `"SET NAMES utf8"` before retrieving data. – Aziz Nov 18 '12 at 10:35
  • There are everal steps where things _can_ go wrong if any component of the tool chain is configured _not_ to use UTF-8. So in the end you will have to check at all stations where the data travels through to find that place where the encoding is suddenly broken. – arkascha Nov 18 '12 at 10:39
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Nov 15 '19 at 04:55

1 Answers1

51

you must set charset in first connect with mysql by this query:

SET CHARACTER SET utf8

for example in mysqli functions

$MySQL_Handle = mysqli_connect(HOSTNAME,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME) 
or die ( mysqli_error($MySQL_Handle) ); 

$sSQL= 'SET CHARACTER SET utf8'; 

mysqli_query($MySQL_Handle,$sSQL) 
or die ('Can\'t charset in DataBase'); 

and PDO sample :

$dbh = new PDO('mysql:host=localhost;dbname=' . $DB_NAME, $DB_USER,
$DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$dbh->exec("SET CHARACTER SET UTF8");

this action need before insert and before select.

A1Gard
  • 4,070
  • 4
  • 31
  • 55