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.
To save an Arabic text into your MySql database table, you first check the table column is set to "utf8" or not. If not, switch to table structure tab of phpmyadmin and click change under action column, here you will see a column "Collation" and choose "utf8_general_ci" from utf8 group and save.
To insert data into a MySQL table, you would need to use the SQL INSERT INTO command. You can insert data into the MySQL table by using the mysql> prompt or by using any script like PHP.
When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.
You have to do both of the following:
utf8_general_ci
(both for the field itself and the table as well as the database).Send two commands right after establishing a connection to the database:
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
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:
utf8_general_ci
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.
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....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With