Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
 $mysqli->set_charset("utf8");

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?

like image 595
MojtabaSh Avatar asked Sep 28 '22 05:09

MojtabaSh


2 Answers

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:

<?php
$mysqli = new mysqli('localhost', 'root', '', 'persian');
$mysqli->set_charset("utf8");
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;?>
like image 138
odedta Avatar answered Oct 02 '22 13:10

odedta


$mysqli->set_charset("utf8"); header("Content-Type: text/html;charset=UTF-8");

instead of $mysqli->set_charset("utf8"); try $mysqli->set_charset("windows-1256");

like image 41
afshin Avatar answered Oct 02 '22 13:10

afshin