How to store the data in unicode in hindi language

醉酒当歌 提交于 2019-11-26 07:39:40

问题


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.


回答1:


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*




回答2:


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




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/12435867/how-to-store-the-data-in-unicode-in-hindi-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!