1

When I work with PHP code I can store Arabic data in MySQL database successfully but get the data back with wrong format. Please note the following :

  1. in my config file I have the below code:

    $this->conn->exec("SET CHARACTER SET utf8");
    
  2. the header has been changed as the below :

    header('Content-Type: text/html; charset=UTF-8');
    
halfer
  • 18,701
  • 13
  • 79
  • 158
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Iłya Bursov Mar 27 '15 at 19:05

3 Answers3

1

Encoding should change at Three points:

  1. Not utf8 from beginning.
  2. PDO connection change it to default.
  3. Table / DB encoding not utf8.

Here we will check option #1

First check your text encoding

$text = "أهلا وسهلا!";    
var_dump(mb_detect_encoding($text));

put your text into $text variable and run this script in browser (Just for encoding check) mb_detect_encoding function should let you know text encoding.

After that you know the encoding you will can use iconv function and convert it. And than, use iconv function to convert to UTF8, For example:

if(mb_detect_encoding($text)!== "UTF-8")
    $decoded_text = iconv(mb_detect_encoding($text), 'UTF-8', $text);
4EACH
  • 1,540
  • 3
  • 14
  • 24
1

you should make all encoding format the same

follow the following steps:

1-your db table should have the utf8_general_ci encoding

2-your sql queries should be done with the same queries by putting the following after mysqli connection in the conifg file

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

3-your page should have the same meta character encoding

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

follow the previous 3 steps and everything will go well

Mohamed Amin
  • 569
  • 5
  • 24
  • this is already done from the first moment which is db created i have export the db and opened it with editor and i noticed that all tables has been created with collation (utf8) but when i tried to check the result which returned from db i found the below : string(5) "ASCII" please note that all tables and db has been check with the below result : default-character-set = utf8 character-set-server = utf8 collation-server = utf8_general_ci init_connect = 'SET collation_connection = utf8_general_ci' init_connect = 'SET NAMES utf8' – Ahmad Alqashqesh Mar 27 '15 at 20:07
0

To properly accept, store, and display multi-byte character sets you have to ensure three things: the database column is using the proper data type, the script was written using multi-byte safe functions, and the output is formatted properly.

In MySQL, you need to store your arabic text in columns of type utf8mb4.

If you use any text editing or formatting in your script, the script needs to both be written in a multi-byte aware editor and use multi-byte safe functions.

And finally your webserver needs to send utf-8 and the browser needs to know to display utf-8 - see How to best configure PHP to handle a UTF-8 website

Community
  • 1
  • 1
K.A.F.
  • 1,989
  • 1
  • 13
  • 17