存储和显示unicode字符串(हिन्दी)使用PHP和MySQL

时间:2022-01-24 21:22:12

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:

我必须在MySQL数据库中存储印地语文本,使用PHP脚本获取并在网页上显示。我做了以下几点:

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.

我创建了一个数据库,并将其编码设置为UTF-8,并将其编码到utf8_bin。我在表中添加了一个varchar字段,并将其设置为在charset属性中接受UTF-8文本。

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

然后我开始向它添加数据。在这里,我必须从现有站点复制数据。北印度语的文本是这样的:सूर्योदय:05:30

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 "??????".

我直接将该文本复制到数据库中,并使用PHP代码echo(utf8_encode($string))显示数据。浏览器向我展示了“?????? ?”

When I inserted the UTF equivalent of the text by going to "view source" in the browser, however, सूर्योदय translates into सूर्योदय.

当我插入文本的UTF相当于通过在浏览器中“查看源代码”,然而,सूर्योदय转化为& # 2360;& # 2370;& # 2352;& # 2381;& # 2351;& # 2379;& # 2342;& # 2351;。

If I enter and store सूर्योदय in the database, it converts perfectly.

如果我输入和存储& # 2360;& # 2370;& # 2352;& # 2381;& # 2351;& # 2379;& # 2342;& # 2351;在数据库中,它可以完美地转换。

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.

所以我想知道的是我如何可以直接存储सूर्योदय进我的数据库,获取并显示它在我的网页使用PHP。

Also, can anyone help me understand if there's a script which when I type in सूर्योदय, gives me सूर्योदय?

还有,谁能帮助我了解如果有一个脚本,当我输入सूर्योदय,给我& # 2360;& # 2370;& # 2352;& # 2381;& # 2351;& # 2379;& # 2342;& # 2351;?

Solution Found

发现解决方案

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>

The dump for my database storing hindi utf strings is

存储印地语utf字符串的数据库的转储是

CREATE TABLE `hindi` (
  `data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `hindi` VALUES ('सूर्योदय');

Now my question is, how did it work without specifying "META" or header info?

现在我的问题是,在没有指定“META”或标题信息的情况下,它是如何工作的?

Thanks!

谢谢!

4 个解决方案

#1


35  

Did you set proper charset in the HTML Head section?

您在HTML头部分设置了适当的字符集吗?

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

or you can set content type in your php script using -

或者您可以使用-在php脚本中设置内容类型

   header( 'Content-Type: text/html; charset=utf-8' ); 

There are already some discussions here on * - please have a look

这里已经有一些关于*的讨论——请看看。

How to make MySQL handle UTF-8 properly setting utf8 with mysql through php

如何让MySQL处理UTF-8,通过php正确设置utf8

PHP/MySQL with encoding problems

PHP / MySQL的编码问题

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.

所以我想知道的是我如何直接存储सूर्योदय进我的数据库,获取并显示在我的网页使用PHP。

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.

我不知道你说的“直接存储在数据库中”是什么意思。您是说使用phmypadmin或其他类似工具输入数据吗?如果是,我尝试过使用PhpMyAdmin来输入unicode数据,所以它对我来说很有用——您可以尝试使用PhpMyAdmin输入数据,并使用php脚本检索数据以进行确认。如果需要通过Php脚本提交数据,请在创建mysql连接时、执行插入查询之前和选择数据时设置名称和字符集。看一下上面的文章,找到语法。希望它可以帮助。

** UPDATE ** Just fixed some typos etc

** *更新**刚刚修复了一些拼写错误等

#2


21  

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


<?php 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

mysql_select_db('onlinetest',$con);

$nith = "CREATE TABLE IF NOT EXISTS `TAMIL` (
  `data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";

if (!mysql_query($nith,$con))
{
  die('Error: ' . mysql_error());
}

$nithi = "INSERT INTO `TAMIL` VALUES ('இந்தியா நாட்டின் பக்கங்கள்')";

if (!mysql_query($nithi,$con))
{
  die('Error: ' . mysql_error());
}

$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
while($myrow = mysql_fetch_row($result))
{
    echo ($myrow[0]);
}
?>
</body>
</html>

#3


3  

For those who are looking for PHP ( >5.3.5 ) PDO statement, we can set charset as per below:

对于那些正在寻找PHP (>5.3.5) PDO语句的人,我们可以设置如下所示的字符集:

$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

#4


1  

CREATE DATABASE hindi_test
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
USE hindi_test;
CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `hindi` (`data`) VALUES('कंप्यूटर');

#1


35  

Did you set proper charset in the HTML Head section?

您在HTML头部分设置了适当的字符集吗?

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

or you can set content type in your php script using -

或者您可以使用-在php脚本中设置内容类型

   header( 'Content-Type: text/html; charset=utf-8' ); 

There are already some discussions here on * - please have a look

这里已经有一些关于*的讨论——请看看。

How to make MySQL handle UTF-8 properly setting utf8 with mysql through php

如何让MySQL处理UTF-8,通过php正确设置utf8

PHP/MySQL with encoding problems

PHP / MySQL的编码问题

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.

所以我想知道的是我如何直接存储सूर्योदय进我的数据库,获取并显示在我的网页使用PHP。

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.

我不知道你说的“直接存储在数据库中”是什么意思。您是说使用phmypadmin或其他类似工具输入数据吗?如果是,我尝试过使用PhpMyAdmin来输入unicode数据,所以它对我来说很有用——您可以尝试使用PhpMyAdmin输入数据,并使用php脚本检索数据以进行确认。如果需要通过Php脚本提交数据,请在创建mysql连接时、执行插入查询之前和选择数据时设置名称和字符集。看一下上面的文章,找到语法。希望它可以帮助。

** UPDATE ** Just fixed some typos etc

** *更新**刚刚修复了一些拼写错误等

#2


21  

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


<?php 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

mysql_select_db('onlinetest',$con);

$nith = "CREATE TABLE IF NOT EXISTS `TAMIL` (
  `data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";

if (!mysql_query($nith,$con))
{
  die('Error: ' . mysql_error());
}

$nithi = "INSERT INTO `TAMIL` VALUES ('இந்தியா நாட்டின் பக்கங்கள்')";

if (!mysql_query($nithi,$con))
{
  die('Error: ' . mysql_error());
}

$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
while($myrow = mysql_fetch_row($result))
{
    echo ($myrow[0]);
}
?>
</body>
</html>

#3


3  

For those who are looking for PHP ( >5.3.5 ) PDO statement, we can set charset as per below:

对于那些正在寻找PHP (>5.3.5) PDO语句的人,我们可以设置如下所示的字符集:

$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

#4


1  

CREATE DATABASE hindi_test
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
USE hindi_test;
CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `hindi` (`data`) VALUES('कंप्यूटर');