How to store the data in unicode in hindi language
Asked Answered
F

4

14

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.

Fruitful answered 15/9, 2012 at 8:38 Comment(1)
read this: #11014037Luralurch
A
26

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*

Aliaalias answered 15/9, 2012 at 8:46 Comment(11)
thanks raju i had set that all thing but i have problem like 1) Which is proper format data should store in mysql in readable format or other format like : (a) नवीन (b) निलेस र पà and second thing while taking text from input box we get data in non reable format so how to convert itFruitful
@Fruitful - you can store data in normal format (as in the case of plain text). If you are using any third party application (which does not support unicode) to view data in your database, you can see corrupted data, but actually they are safe in mysql! see my update: You need to set character set on both mysql side and browser side.Aliaalias
thanks @raju, well iam using mysql database and front end php and now when iam typing in text box "किसानों के लिये एक नवीन उपकरण।" its storing in database in this format "किसानों के लिये à¤à¤• नवीन उपकरण।" and when iam retriving its showing proper again in "किसानों के लिये एक नवीन उपकरण।" so i just want to know is there any function that i can store directly in readable format "किसानों के लिये एक नवीन उपकरण।Fruitful
It looks like proper UTF-8 by quick glance, you just need to use a tool which supports this encoding (you seem to be using a tool which is limited to, or defaults to, something like ISO-8859-1).Through
@Fruitful - Actually your data is stored in readable format itself. What tripleee said is absolutely correct. The tool you are used to view data might not have full unicode support. BTW, which tool showed you the text "किसानों के लिये à¤à¤• नवीन उपकरण।" ?Aliaalias
@raju thanks so you think data which is store is perfect no change is required know because we are going to enter more then 10000 entries so do you know any function or tools which supportFruitful
@triplee thanks friend do you know any encoding tool which supportFruitful
@Fruitful - I had developed an application which uses some phrases in 41 different languages, and I used PHP+MySQL (no other tools used for any conversion process). It doesn't matter whether you've 10K or 20K entries. And one important thing is that, I've changed mysql_query("SET CHARACTER SET utf8") to mysql_set_charset('utf8'); for performance improvement. I'm using PHPMyAdmin to handle with unicode characters, and SQLyog for normal operation (Since SQLyog doesn't have full unicode support).Aliaalias
@raju just last question you know my current condition and the format in which data its store and retrive so is it ok should i move forward with this condition anything need to know then let me knowFruitful
@Fruitful - If you've successfully inserted and retrieved just one record, then definitely you can use same method to insert and/or retrieve 10000+ records... Keep i mind that you should have to set proper character set before making any query to MySQL... (sorry for the late reply)Aliaalias
Please note if you are using mysqli mysqli_set_charset($db_conn,'utf-8');Medarda
S
3

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

Shinto answered 6/8, 2014 at 11:53 Comment(0)
K
1

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);
Kanaka answered 9/10, 2016 at 8:16 Comment(1)
you are right, it's worked, I used Storage Engin "myISAM" & Collation "utfmb4_unicode_520_ci" in mysql and used mysqli_set_charset($con ,'utf8'); in my php.Kero
H
1

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);

Haematocryal answered 27/6, 2018 at 13:36 Comment(1)
You need to mention specific aspect to take care, in this case, including the encoding as 'utf-8' to get unicode.Sumer

© 2022 - 2024 — McMap. All rights reserved.