使用 PHP 和 MySQL 存储和显示 unicode 字符串 (??????)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1198701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and displaying unicode string (??????) using PHP and MySQL
提问by Anirudh Goel
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 等效项时,???????? 翻译成सूर्योदय.
If I enter and store सूर्योदयin the database, it converts perfectly.
如果我输入并存储सूर्योदय在数据库中,它会完美转换。
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 सूर्योदय?
另外,任何人都可以帮助我理解是否有一个脚本,当我输入 ???????? 时,会给我सूर्योदय?
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!
谢谢!
采纳答案by TigerTiger
Did you set proper charset in the HTML Head section?
您是否在 HTML Head 部分设置了正确的字符集?
<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 StackOverflow - please have a look
这里已经有一些关于 StackOverflow 的讨论 - 请看一看
How to make MySQL handle UTF-8 properlysetting utf8 with mysql through php
如何让 MySQL 处理 UTF-8 正确设置 utf8 与 mysql 通过 php
PHP/MySQL with encoding problems
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.
我不确定您所说的“直接存储在数据库中”是什么意思..您的意思是使用 PhpMyAdmin 或任何其他类似工具输入数据吗?如果是,我已经尝试使用 PhpMyAdmin 输入 unicode 数据,所以它对我来说效果很好 - 您可以尝试使用 phpmyadmin 输入数据并使用 php 脚本检索它以确认。如果您需要通过 Php 脚本提交数据,只需在创建 mysql 连接时、执行插入查询之前以及选择数据时设置 NAMES 和 CHARACTER SET。查看上面的帖子以找到语法。希望能帮助到你。
** UPDATE ** Just fixed some typos etc
** 更新 ** 只是修正了一些错别字等
回答by ROSE
<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>
回答by Sandeep
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');
回答by Vinod Tiwari
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('????????');
回答by Aklesh Singh
For Those who are facing difficulty just got to php admin and change collation to utf8_general_ciSelect Table go to Operations>> table options>> collations should be there
对于那些遇到困难的人,只需进入 php admin 并将排序规则更改为utf8_general_ci选择表转到操作>>表选项>>排序规则应该在那里

