使用 PHP 和 MySQL 存储和显示 unicode 字符串 (हिन्दी) [英] Storing and displaying unicode string (हिन्दी) using PHP and MySQL

查看:34
本文介绍了使用 PHP 和 MySQL 存储和显示 unicode 字符串 (हिन्दी)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将印地文文本存储在 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/MySQL 存在编码问题

所以我想知道我如何才能直接将सूर्योदय存入我的数据库并获取它并显示在我的网页使用 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 连接时、在执行插入查询之前以及选择数据时设置 NAMES 和 CHARACTER SET.查看上面的帖子以找到语法.希望有帮助.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆