MojtabaSh MojtabaSh - 5 months ago 38
SQL Question

How to read a Persian string from MySql in PHP?

I inserted persian string to my table rows in my database, where my columns collection is set to utf8mb4_persian_ci.

But when i want read data from MySql database to php , it doesn't show persian string correctly and just shows ? ? ? ?

I read many article for fix this problem but it didn't work for me
I used this code after connecting to database

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

And this code at header of php codes

header("Content-Type: text/html;charset=UTF-8");

And this code at top of html codes

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

But it don't work and I can't see persian string in php pages where it's read from MySql to php

How to fix this problem?


This is the code I used to get this to work properly, since I used all your code, this means your mysql database configuration is incorrect, please recreate your database with the right collation, make sure the table collation is also the same and the field type to be TEXT

Here is the code:

$mysqli = new mysqli('localhost', 'root', '', 'persian');
header("Content-Type: text/html;charset=UTF-8");
$mysqli->query("INSERT INTO user SET name='فارسی / پارسی'");
$result = $mysqli->query("SELECT * FROM user");
$result = $result->fetch_object();
echo '<pre dir="ltr">';var_dump($result);echo '</pre>';
echo $result->name;?>