PHP MYSQL Insert Data in Arabic

I am trying to insert some Arabic language data into MySQL using PHP and an HTML form. When I insert data into a MYSQL table, the table field represents the data as Ù…Ø±ØØ¨Ø§ العالم

But when I access the same data with PHP and display it on my web page, it shows the correct data. I use:

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

meta on my web page to display arab data. My question is why my data looks like this: Ù…Ø±ØØ¨Ø§ العالم in the MySQL table and how to fix it.

+4
source share
6 answers

You need to do the following:

  • Make sure your encoding and database mapping is utf8_general_ci (both for the field itself, for the table, and also for the database).
  • Send two commands immediately after establishing a connection to the database:

     mysql_query("SET NAMES utf8;"); mysql_query("SET CHARACTER_SET utf8;"); 
+17
source

You should use this line:

 @mysql_query("SET NAMES 'utf8' "); 

See this function:

 function _connect($user, $pass, $host) { $conn = mysql_connect($host, $user, $pass); if (!$conn) return false; @mysql_query("SET NAMES 'utf8' "); //more.... } 
+1
source

You do not mention the version of MySQL you are using, but if you are using 5.0.7 or later, according to the official PHP documentation

This is the preferred way to change the encoding. Using mysql_query () to install it (e.g. SET NAMES utf8) is not recommended. For more information, see MySQL Character Set Concepts.

, for example, Assuming you are using the mysql_query extension.

 <?php $link = mysql_connect('localhost','user1','pass1',TRUE); mysql_selectdb('db1', $link); mysql_set_charset('utf8',$link); ?> 

Other considerations:

  • Files used must be encoded using UTF-8
  • The entire database, table and field must be encoded using utf8_general_ci
  • PHP headers and HTML headers must also be set to UTF-8

As a side note, using the mysql_query extension is not recommended. Instead, use the MySQLi extension or PDO_MySQL ..

See also MySQL: API guide selection and related FAQ for more information.

+1
source

Your database must be supported by utf8 . Try using utf8_general_ci or utf8_unicode_ci

0
source

try the following:

 @mysql_query("set global character_set_results=utf8"); 
0
source

Now PHP and MYSQL support any language. Use this function when connecting to the database.

 $this->links = mysql_connect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD); mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $this->links); 

MYSQL database table field should be Collation = utf8_general_ci

-1
source

All Articles