Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Displaying Urdu text retrieved from MySQL DB

There are book titles in Urdu language stored in MySQL database. I've to display on html page using PHP.

Currently only questions marks(??????) are displayed in place of Urdu text.

<div class='product_title'><a href='details.php?pid=".$Row['s_id']."'>".$Row["books"]."</a></div>

What needs to be done to display these characters properly?

like image 844
sarah Avatar asked Jul 01 '12 08:07

sarah


1 Answers

I faced the same issue and solved by manipulating the character encoding of the entire PHP-MySQL environment.

By Default the character set encoding in PHPMyAdmin is generally Latin Swedish or utf8 general, However, other languages like Urdu, Chinese and more are not supported in the character set encoding, So you need to change the character encoding of your MySQL database or table or column as per your requirement.

# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
# (Don’t blindly copy-paste this! The exact statement depends on the column type, maximum length, and other properties. The above line is just an example for a `VARCHAR` column.)

You also need to set the character encoding for the data retrieved using mysqli_query. You can do so by using mysqli_charset() function just below the connection line. Like...

 // Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

/* change character set to utf8mb4 */
mysqli_set_charset($conn,"utf8mb4");

Now You can display Urdu or any other Language directly by retrieving from MySQL database through PHP Query.

like image 110
Monil Bhavsar Avatar answered Oct 30 '22 00:10

Monil Bhavsar