Thursday, July 5, 2012

Working with Foreign Languages using MySQL and PHP.

Database
Sample database articles table columns idtitle and description. Character set should be UTF-8 format. 
CREATE TABLE `articles
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(300) CHARACTER SET utf8 collate utf8_general_ci ,
`description` text CHARACTER SET utf8 collate utf8_general_ci,
PRIMARY KEY (`id`)
)

You can set this using PHPMyAdmin. Go to table structure and change collationlatin1_swedish_ci to utf8_general_ci
MySQL Character set

Insert - Why prefix 'N'?
SQL insert statement. Here the N stands for National language character set. Which means that you are passing an NCHAR, NVARCHAR or NTEXT value Read more

INSERT INTO 
articles(title,description
VALUES 
(N'శ్రీనివాస్ తామాడా', N'新概念英语第');

HTML META TAG
You have to include this following tag in data results pages.

Useful Related Posts
Regional Language UTF-8 Text Box
Database Searching Techniques Regional Language. 

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

Displaying Records
Contains PHP code displaying records form database. Before that you have to specify mysql_query() function data character set type. 

<?php
include('db.php');
mysql_query ("set character_set_results='utf8'"); 
$query = mysql_query("SELECT * FROM articles") or die(mysql_error());
while($row=mysql_fetch_array($query))
{
echo $row['title']; // Article 
echo $row['description']; // Description
}
?>

No comments:

Post a Comment