7

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
h_h
  • 1,201
  • 4
  • 28
  • 47
  • 1
    possible duplicate of [PHP Mysql Json and Arabic language](http://stackoverflow.com/questions/11262561/php-mysql-json-and-arabic-language) – eggyal Jun 29 '12 at 14:32
  • 1
    I have found this article to be helpful: http://www.bluebox.net/news/2009/07/mysql_encoding/ – Mark Eirich Jun 29 '12 at 14:43

8 Answers8

20

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;");
    
hakre
  • 193,403
  • 52
  • 435
  • 836
Ansari
  • 8,168
  • 2
  • 23
  • 34
3

You aren't mentioning the MySQL version that you are using, but if using 5.0.7 or later, as per the official PHP documentation:

This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See the MySQL character set concepts section for more information.

e.g., Assuming that 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:

  • The files being used should be encoded with UTF-8
  • The database, table and field should all be encoded and using the collation utf8_general_ci
  • The PHP headers and HTML headers should also be set to UTF-8

As a side note, The use of mysql_query extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

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

Zuul
  • 16,217
  • 6
  • 61
  • 88
2

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....
}
Soroush Khosravi
  • 887
  • 2
  • 11
  • 30
0

Your database should be utf8 supported. Try with utf8_general_ci or utf8_unicode_ci

Adnan D.
  • 430
  • 1
  • 5
  • 19
nbhatti2001
  • 353
  • 2
  • 7
  • 33
0

try this:

@mysql_query("set global character_set_results=utf8");
Taz
  • 3,718
  • 2
  • 37
  • 59
luchopintado
  • 899
  • 11
  • 15
0

try to change server and db and table collation

for example

ALTER DATABASE "DBNAME" CHARACTER SET utf8 COLLATE utf8_general_ci;

  • Please add some explanation to your answer such that others can learn from it - are you sure this is only a problem on the database side? – Nico Haase May 27 '20 at 05:58
0

The following works for me:

mysqli_set_charset($this->connect,'utf8'); mysqli_query($this->connect, "SET NAMES 'utf8'"); mysqli_query($this->connect, 'SET CHARACTER SET utf8');

Moreover, your table must be (utf8_general_ci) in the structure field.

Aditya Prakash
  • 1,549
  • 2
  • 15
  • 31
ahmad almasri
  • 59
  • 3
  • 11
-1

Now PHP and MYSQL support any language. Use this function in database connectivity .

$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