为什么我的html不显示从数据库中提取的特殊字符 [英] why does my html not display special characters taken from my database

查看:196
本文介绍了为什么我的html不显示从数据库中提取的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的php文件的顶部包括了这个

I included this at the top of my php file:

<?php
    header('Content-Type: text/html; charset=UTF-8');
?>

之所以这样做,是因为我的file.php在html文件或数据库查询的数据中未显示á,é,í,ó,ú或¿".

I did this because my file.php was not displaying "á, é, í, ó, ú or ¿" in the html file or from data queried from my database.

放置'header('Content-Type:text/html; charset = UTF-8');'后,我的html页面的一行代码开始了解html文件中的特殊字符,但是,从我的数据库接收到的数据现在带有带有问号的黑色菱形.

After I placed the 'header('Content-Type: text/html; charset=UTF-8');' line of code my html page started to understand the special characters in the html file but, data received from my database now has a black rhombus with a question mark.

我的数据库的归类为"utf8_spanish_ci"

The collation my database has is "utf8_spanish_ci"

在html标签上,我尝试将lang = es放进去,但这从未奏效,我也试图将meta标签放到head标签内

at the html tag i tried to put lang=es but this never worked I also tried to put the meta tag inside the head tag

<!DOCTYPE html>
<html lang=es>
<head>
     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>

我也尝试过:

<meta charset="utf-8">

和:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

我不知道问题是什么.当我将数据直接插入数据库时​​,特殊字符在那里,但是当我从file.php中插入特殊字符时,它们会出现随机字符.

I don't know what the problem is. When I insert data directly into the data base the special characters are there but when I insert them from my file.php they appear with random characters.

有人知道为什么会这样吗?

Does anyone know why this is happening?

推荐答案

有两种可能的原因.但是,很重要的一点是,您的整个代码行都使用相同的字符集,并且所有可以设置为特定字符集的函数都应设置为相同.使用最广泛的一种是UTF-8,这是我建议您使用的一种.

There are a couple of reasons this could be happening. It is however important that your entire line of code uses the same set of charset, and that all functions that can be set to a specific charset, is set to the same. The most widely used one is UTF-8, which is the one I'm suggesting you use.

连接

  • 您还需要在连接本身中指定字符集.
    • PDO (在对象本身中指定):
      $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));
    • MySQLi :(在创建连接后直接放置)
      *对于OOP:$mysqli->set_charset("utf8");
      *对于程序:mysqli_set_charset($mysqli, "utf8");
      (其中$mysqli是MySQLi连接)
    • MySQL (已描述,您应该转换为PDO或MySQLi):(在创建连接后直接放置)
      mysql_set_charset("utf8");
    • You also need to specify the charset in the connection itself.
      • PDO (specified in the object itself):
        $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));
      • MySQLi: (placed directly after creating the connection)
        * For OOP: $mysqli->set_charset("utf8");
        * For procedural: mysqli_set_charset($mysqli, "utf8");
        (where $mysqli is the MySQLi connection)
      • MySQL (depricated, you should convert to PDO or MySQLi): (placed directly after creating the connection)
        mysql_set_charset("utf8");

      数据库

      • 您的数据库及其所有表都必须设置为UTF-8.请注意,字符集与排序规则相同.

      • Your database and all its tables has to be set to UTF-8. Note that charset is not the same as collation.

      您可以通过对每个数据库和表(例如在phpMyAdmin中)在一次下运行查询来做到这一点

      You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)

      ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
      ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

      文件编码

      • .php文件本身也是UTF-8编码,这一点也很重要.如果您使用的是Notepad ++编写代码,则可以在任务栏上的格式"下拉菜单中完成此操作(转换为不带BOM的UFT-8 ).您应该使用UTF-8 w/o BOM.
      • It's also important that the .php file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar (Convert to UFT-8 w/o BOM). You should use UTF-8 w/o BOM.

      如果您按照上面的所有指示进行操作,很可能会解决您的问题.如果不是这样,您可以查看以下StackOverflow帖子: UTF-8贯穿始终.

      Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

      这篇关于为什么我的html不显示从数据库中提取的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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