【发布时间】:2016-04-30 02:43:45
【问题描述】:
我在数据库中有一些文本。我使用法语和英语。法语有口音和一些特殊字符,如ç。我使用 Mamp、MySQL 和 PHP。
我有排序规则latin1_swedish-ci(默认)。我试过utf8_general_ci,结果是一样的。
如果我在 html 页面中使用,我会在脑海中看到这个:<meta charset="UTF-8">
例如,在数据库中我有“voilà”。
当我将文本从数据库回显到 html 时:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
- 为什么
htmlentities不起作用? -
utf8_encode();是要走的路吗?当我从数据库输出一些东西时,我必须总是使用它吗?如果排序规则已经是UTF8,为什么我必须使用它?有没有更好的方法在 MySQL 数据库中存储和输出带有重音符号的文本?
【问题讨论】:
-
@ItsGreg。另一个不回答我的问题。我得到了一个类似的解决方案,但问题是为什么
-
如果您的字符串是
voilà,htmlentities()不应返回任何内容。 phpMyAdmin 中的字段是什么样的? -
@rybo111:在 phpMyAdmin 我只有那个:瞧。只是为了测试。为什么它什么都不返回?
-
您可以在查询之前尝试
mysqli_set_charset($con,"utf8");,$con是您的连接吗?
标签: php mysql html-entities