Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the data in unicode in hindi language

I am using PHP and MySQL for an application.

Questions are:

  1. How to store data in MySQL नवीन खेतिहर उपकरण। readable format or निलेस र पà¥à¤¬ format

  2. When user enters data in a textbox and clicks on submit, at that time we get data in different format. What we need to do should we convert and store in MySQL in readable format.

like image 839
nilesh Avatar asked Sep 15 '12 08:09

nilesh


4 Answers

Choose utf8 character set and utf8_general_ci collation.

Obviously, Collation of the field (to which you want to store Hindi text) should be utf8_general_ci.

To alter your table field, run

ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100) 
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;

Once you've connected to database, run the following statement at first

mysql_set_charset('utf8');

Eg:

//setting character set
mysql_set_charset('utf8');

//insert Hindi text
mysql_query("INSERT INTO ....");

To retrieve data

//setting character set
mysql_set_charset('utf8');

//select Hindi text
mysql_query("SELECT * FROM ....");

Before you printing any unicode text (say Hindi text) on browser, you should have to set content type of that page by adding a meta tag

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

Eg:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Unicode</title>
</head>

<body>
<?php echo $hindiText; ?>
</body>
</html>

Update:

mysql_query("SET CHARACTER SET utf8") has changed tomysql_set_charset('utf8'); This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See http://php.net/manual/en/function.mysql-set-charset.php*

like image 62
rajukoyilandy Avatar answered Nov 20 '22 14:11

rajukoyilandy


After converting COLLATE to utf8_general_ci use this line mysqli_set_charset($con, 'utf8'); in between $con and mysqli_query() below your query.

$con= mysqli_connect($host,$user,$pass,$db) or die('Unable to connect');

$sql="your query.. ";

mysqli_set_charset($con, 'utf8');

$result=mysqli_query($con,$sql);

like image 42
Jiten Basnet Avatar answered Nov 20 '22 12:11

Jiten Basnet


You only need to run the following command on the table.

ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Note: You should use utf8_general_ci instead of ut8_unicode_ci, as it is said to be faster. What's the difference between utf8_general_ci and utf8_unicode_ci

like image 3
Sorter Avatar answered Nov 20 '22 13:11

Sorter


There is no need to alter table with charset. but your database should be with utf8_unicode_ci.

while connecting with database just need to add this line only." mysqli_set_charset($con, 'utf8')"

    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    mysqli_set_charset($con, 'utf8');
    mysqli_query($con, $sql);
like image 1
Dinesh Sarak Avatar answered Nov 20 '22 13:11

Dinesh Sarak