How to make PHP and MySQL support Arabic, Urdu and charactersets or other foreign languages

To enable your PHP/MySQL script support Arabic or Urdu or other foreign languages, you need to do the following.

In database connection, do the following.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
//Below is an additional line to enable support for foreign languages.
mysql_query(“SET NAMES utf8″) ;
mysql_select_db($db_name,$cn) or die(mysql_error());

In database, set collation of fields to utf8_general_ci. If you can set collation of databas and tables to utg8_general_ci as well, that will even be better. A sample database script is given below.


– Database: `mytest`

CREATE DATABASE `mytest` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mytest`;

– ——————————————————–


– Table structure for table `mytable`

CREATE TABLE IF NOT EXISTS `mytable` (
`my_id` int(11) NOT NULL auto_increment,
`my_title` varchar(100) NOT NULL,
PRIMARY KEY (`my_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Now you need to set the charset of the page. Add the following meta tag to the head of you web page. If a charset meta tag already exists, then change it to as below.

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

That’s it!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s