Php – Store Arabic text in thesql database using php

character encodingMySQLmysqliPHPutf-8

I am trying to store some Arabic data in a mysql database
I have set the html document charset to be 'utf8'

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

MySQL charset is set to: UTF-8 Unicode (utf8)

MySQL connection collation is set to: utf8_general_ci

Database and table collations are set to: utf8_general_ci

In addition in my php code I used the $mysqli->set_charset("utf8") function to ensure that the charset is set to be ut8 but nothing is actually working , I am posting my data to the php script using html form where the enctype is : enctype="multipart/form-data" because in this form I also upload an image

The weird thing is that when I write my query directly in mysql the Arabic characters are stored properly with no problem , but when I use a php query to store the data all the characters are stored in a wrong charset encoding

Any suggestions ?

Best Answer

Weird, It should work!

Try adding these lines after a successful connection to the database:

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


For example:

$mysqli = @new mysqli('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');

if($mysqli->connect_errno) die('Connection Error!');
else{
    $mysqli->query("SET NAMES 'utf8'");
    $mysqli->query("SET CHARACTER SET utf8");
}


And include these META tags in the head section of your php page:

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