Tip: Display unicode data from mySQL DB on a website in PHP.

Before we start we need to make mySQL database capable of supporting unicode characters. For that we need to change the database collation or the table collation to utf8_unicode_ci. This can be done with phpmyadmin portal.

  1. Click the table you need to change.
  2. select Structure tab.
  3. Check the required column and click change or edit.
  4. From the collation drop down select utf8_unicode_ci.
  5. Press Go.

Now your mySQL database table column is unicode supported. You can insert some unicode values via phpmyadmin itself. Just copy and paste unicode text from some websites like http://www.manoramaonline.com and submit. Ok we got our unicode text inserted into our unicode field in a table.

Next we will see how to display that content on the webpage using php.

Below is the normal command to connect with our mySQL database.

mysql_connect(“host”, “username”, “password”);
mysql_select_db(“$db_name”);

After the above command insert below lines and our mission is accomplished.

header(‘content-type:text/html; charset=utf-8’);
mysql_query(“set names utf8”);

Now the data retrieved and displayed with standard commands will display those unicode characters properly.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.