PHP MYSQL Insert Data in Arabic Language

h_h picture h_h · Jun 29, 2012 · Viewed 12.8k times · Source

I am trying to insert some Arabic Language data into MySQL using PHP and an HTML form. When I insert the data in to MYSQL table, the table field represents data as مرحبا العالم.

But when I access the same data with PHP and show it in my webpage, it shows the correct data. I am using:

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

meta tag in my web page to show Arabic data. My question is why my data looks like this: مرحبا العالم in MySQL table, and how should I correct it.

Answer

Ansari picture Ansari · Jun 29, 2012

You have to do both of the following:

  1. Make sure your database encoding and collation is utf8_general_ci (both for the field itself and the table as well as the database).
  2. Send two commands right after establishing a connection to the database:

    mysql_query("SET NAMES utf8;");
    
    mysql_query("SET CHARACTER_SET utf8;");