使用PHP和MySQL存储和显示Unicode字符串(हिन्दी) [英] Storing and displaying unicode string (हिन्दी) using PHP and MySQL
问题描述
我必须将印地文文本存储在MySQL数据库中,使用PHP脚本进行获取并将其显示在网页上.我做了以下事情:
I have to store hindi text in a MySQL database, fetch it using a PHP script and display it on a webpage. I did the following:
我创建了一个数据库,并将其编码设置为UTF-8,并将排序规则设置为utf8_bin
.
我在表中添加了一个varchar字段,并将其设置为在charset属性中接受UTF-8文本.
I created a database and set its encoding to UTF-8 and also the collation to utf8_bin
.
I added a varchar field in the table and set it to accept UTF-8 text in the charset property.
然后我开始向其中添加数据.在这里,我不得不从现有网站. 印地文文本如下所示:सूर्योदय:05:30
Then I set about adding data to it. Here I had to copy data from an existing site. The hindi text looks like this: सूर्योदय:05:30
我直接将此文本复制到我的数据库中,并使用PHP代码echo(utf8_encode($string))
显示数据.这样做后,浏览器向我显示了"??????".
I directly copied this text into my database and used the PHP code echo(utf8_encode($string))
to display the data. Upon doing so the browser showed me "??????".
但是,当我通过在浏览器中转到查看源代码"插入等效于文本的UTF时,सूर्योदय会转换为सूर्योदय
.
When I inserted the UTF equivalent of the text by going to "view source" in the browser, however, सूर्योदय translates into सूर्योदय
.
如果我输入सूर्योदय
并将其存储在数据库中,它将完美转换.
If I enter and store सूर्योदय
in the database, it converts perfectly.
所以我想知道的是如何直接将सूर्योदय存储到我的数据库中,并使用PHP将其提取并显示在我的网页中.
So what I want to know is how I can directly store सूर्योदय into my database and fetch it and display it in my webpage using PHP.
还有,有人能帮我理解是否有一个脚本,当我在सूर्योदय中键入时会给我सूर्योदय
吗?
Also, can anyone help me understand if there's a script which when I type in सूर्योदय, gives me सूर्योदय
?
找到解决方案
我写了以下对我有用的示例脚本.希望它对其他人也有帮助
I wrote the following sample script which worked for me. Hope it helps someone else too
<html>
<head>
<title>Hindi</title></head>
<body>
<?php
include("connection.php"); //simple connection setting
$result = mysql_query("SET NAMES utf8"); //the main trick
$cmd = "select * from hindi";
$result = mysql_query($cmd);
while ($myrow = mysql_fetch_row($result))
{
echo ($myrow[0]);
}
?>
</body>
</html>
用于存储印地语utf字符串的数据库的转储为
The dump for my database storing hindi utf strings is
CREATE TABLE `hindi` (
`data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `hindi` VALUES ('सूर्योदय');
现在我的问题是,如何在不指定"META"或标题信息的情况下工作?
Now my question is, how did it work without specifying "META" or header info?
谢谢!
推荐答案
您是否在HTML Head部分中设置了正确的字符集?
Did you set proper charset in the HTML Head section?
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
或者您可以使用-
header( 'Content-Type: text/html; charset=utf-8' );
关于StackOverflow的讨论已经在这里-请看看
There are already some discussions here on StackOverflow - please have a look
如何使MySQL正确处理UTF-8 一个> 通过php通过mysql设置utf8
所以我想知道的是我该怎么办 直接将सूर्योदय存入我的 数据库并获取它并显示在 我的网页使用PHP.
So what i want to know is how can i directly store सूर्योदय into my database and fetch it and display in my webpage using PHP.
我不确定直接存储在数据库中"是什么意思..您是指使用PhpMyAdmin或任何其他类似工具输入数据吗?如果是,我已经尝试使用PhpMyAdmin输入unicode数据,所以对我来说效果很好-您可以尝试使用phpmyadmin输入数据,然后使用php脚本进行检索以进行确认.如果您需要通过Php脚本提交数据,则只需在创建mysql连接时,执行插入查询之前以及选择数据时设置名称和字符集即可.看看以上文章以查找语法.希望对您有所帮助.
I am not sure what you mean by "directly storing in the database" .. did you mean entering data using PhpMyAdmin or any other similar tool? If yes, I have tried using PhpMyAdmin to input unicode data, so it has worked fine for me - You could try inputting data using phpmyadmin and retrieve it using a php script to confirm. If you need to submit data via a Php script just set the NAMES and CHARACTER SET when you create mysql connection, before execute insert queries, and when you select data. Have a look at the above posts to find the syntax. Hope it helps.
**更新** 只是修正了一些错字等
** UPDATE ** Just fixed some typos etc
这篇关于使用PHP和MySQL存储和显示Unicode字符串(हिन्दी)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!