使用重音来存储和输出文本

时间:2016-01-23 14:28:43

标签: php mysql html-entities

我在数据库中有一些文字。我用法语和英语。法语有重音符号和一些特殊字符,如ç。我使用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à
}
  1. 为什么htmlentities不起作用?
  2. utf8_encode();还有什么路要走?当我从数据库中输出内容时,我总是要使用它?如果排序已经UTF8,为什么必须使用它?有没有更好的方法在MySQL数据库中存储和输出带重音的文本?

1 个答案:

答案 0 :(得分:0)

连接到数据库后,应将客户端字符集设置为UTF8:

mysqli_set_charset($con, "UTF8");

否则mysql客户端会将UTF8'voilà'转换为latin1(因为它似乎是默认设置)。

要么告诉客户我想要UTF8中的所有内容,要么使用默认的latin1来获取它,并自己逐个转换为utf8_encose($ text)

相关问题