mardi 30 août 2011

Working with Foreign Languages using MySQL and PHP.

Posted by YufGrafix | mardi 30 août 2011 | Category: , , , |


I received many tutorial requests from my readers that asked to me how to display and store foreign/regional language UTF-8 data into MySQL database using PHP code. This post explains you to solve Unicode data problems, just there steps you should follow take a quick look at this post.

UTF-8 using MySQL and PHP.

Live Demo

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 specifymysql_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
}
?>

Hope this post helps your web project to reach global and regional(Indian languages). Thanks!

Currently have 0 comments:


Leave a Reply