Multilingual Implementation using PHP MySql
Making Hindi website using PHP MySql
Objective:
Make a web based, database driven application which supports languages other than English. For the trial purpose make an application that takes data in "Hindi" from the web application, saves it in the database, and show the saved data to the user in "Hindi".
|
* The application is not a language translation application. It takes data in regional language and saves it like that. It is neither responsible for the accuracy of the data nor responsible for generating the regional language string. It just accepts the string from the user and saves it into the database. |
Technology:
- PHP 5.2.6
- Notepad++ 5 (or any desired editor)
- MySql 5
- MySql GUI tool 5
- Apache 2.2
- FireFox 3 (or any desired browser)
- Microsoft Windows XP
Prerequisites:
The desired development environment (as given in the Technology section) should be installed and well communicating with each other.
Process:
Database
- First create a database schema with the name "test".
- Then create a user in database “test” with the password “test” and assign right on “test” schema to this user.
- Now, connect to the database and run the below given script to create table.
Table Structure –
CREATE TABLE LANG_TEST
(
ID INTEGER(2) PRIMARY KEY,
NAME VARCHAR(100) CHARACTER SET UTF8
)
- Enter a record into the table to test if MySql supports “Hindi” language. Copy & Paste the below given query using MySql Query Browser (installed through MySql GUI tool 5).

You may not see the Hindi string correctly in MySql Query Browser. To see it properly go to Tools >Options and select "General Option" tab (Screen 1). In "Application Fonts" section set Default Font, Data Font, and Code Font to @Ariel Unicode MS and press "Apply" and "Close". Now, you can see the Hindi string properly.
Screen 1
- Execute the INSERT query.
- Now, run the below given query to retrieve the records. You will see the record as entered by you.
SELECT * FROM LANG_TEST
PHP Code
Let’s now begin with coding for a web based application. As per our requirement we need to first insert “Hindi” string into the database and then retrieve it. For making this possible we will write two different codes namely –
- hindi_entry.php
- hindi_view.php
Kindly note that text marked as RED in the below given code is something that makes our requirement possible.
You can copy the below given code and save it with the given file name.
hindi_entry.php
<html lang="hi">
<head>
<title>Hindi Data entry</title>
<meta http-equiv="Content-Language" content="hi">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
if (isset($_POST['posted']))
{
$dbHost = "localhost";
$dbUser = "test";
$dbPassword = "test";
$dbSchema = "test";
// making database connection
$varLinkId = mysql_connect($dbHost, $dbUser, $dbPassword) or die("cannot connect to the database");
mysql_select_db ($dbSchema, $varLinkId);
// for generating unique number for ID field
$varQuerySel = "SELECT MAX(ID) FROM LANG_TEST";
$varResultSet = mysql_query($varQuerySel, $varLinkId);
$varId = 0;
while ($varId1 = mysql_fetch_row($varResultSet))
{
$varId = $varId1[0];
}
if ($varId == NULL)
{
$varId = 1;
}
else
{
$varId = $varId +1;
}
// Inserting data
$vartxt1 = $_POST['txt1'];
$varQuery = "INSERT INTO LANG_TEST VALUES (" . $varId . ", '" . $vartxt1 . "')";
mysql_query ($varQuery, $varLinkId);
mysql_close($varLinkId);
}
?>
<form name="frm1" method="POST" action="hindi_entry.php">
<input type="hidden" name="posted" value="true" />
Enter Hindi Text : <input type="text" name="txt1" size="20" maxlength = "100" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
|
* The above code may not work in some of the editors as there may be some difference in character transformation while copy & pasting (generally in case of spaces, quotes, and double-quotes). Verify your code after pasting it. |
When you will run the code in the browser you will see the screen as below (Screen 2).

Screen 2
hindi_view.php
<html lang="hi">
<head>
<title>Hindi Data entry</title>
<meta http-equiv="Content-Language" content="hi">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
$dbHost = "localhost";
$dbUser = "test";
$dbPassword = "test";
$dbSchema = "test";
// making database connection
$varLinkId = mysql_connect ($dbHost, $dbUser, $dbPassword) or die("cannot Connect to the database");
mysql_select_db ($dbSchema, $varLinkId);
//generating and running database query
$varQuery = "SELECT ID, NAME FROM LANG_TEST ORDER BY ID";
$varResultSet = mysql_query($varQuery, $varLinkId);
echo "<table border='1'>";
while($varData = mysql_fetch_row($varResultSet))
{
echo "<tr><td>" . $varData[0]. "</td><td>" . $varData[1] . "</td></tr>";
}
echo "</table>";
mysql_close($varLinkId);
?>
</body>
</html>
|
* The above code may not work in some of the editors as there may be some difference in character transformation while copy & pasting (generally in case of spaces, quotes, and double-quotes). Verify your code after pasting it. |
When you will run the code in the browser you will see the screen as below (Screen 3).

Screen 3
Known Issues:
When you insert a record through MySql Query Browser (as in step 4) and then select through it you see the readable data , but when you enter through web application and select through MySql Query Browser then you see the encrypted data.
Feedback:
We would appreciate any constructive feedback on this document. Please let us have your Suggestions, Corrections, or Modification at the given email.
Manu Gupta
contact@growthtechnosoft.com
More Papers... |