Navigation Menu

How to store and retrieve Thaana Text (unicode) with PHP and MySQL?

It is inevitable for every new web developer to run in to issues either storing or retrieving Thaana Text on a MySQL database. In most cases you run in to problems like your stored text appears to be gibberish (eg: the text 'Þ‹ިÞˆެÞ€ި' appears as '??????'!). I faced the same issue and was left to boil my head for hours. So, I thought i'll make it easy for you. With just one line of code, you will be able to overcome the issue! Here I show you how to store and retrieve Thaana Text (or any other unicode text for that matter) successfully.

1. Preliminary Checkups 

A. Check if the table collation is set to "utf8_general_ci" from table operations
B. Check if the .php file is saved in utf8 encoding

These two things are VERY VERY important. Most of the times these are the two things people overlook. I personally spent many hours wondering and pondering about what I was doing wrong until I finally figured out that I saved the .php file in another encoding.

2. The Magic Line

After connecting to the MySQL database, you will need to set the character encoding to UTF8 like this in your PHP code

mysqli_query($con,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

This code tells the script to do all the operations after it in UTF8 character encoding.


0 comments:

Instagram

Follow @ daniel_Ibbe