why does my html not display special characters taken from my database

懵懂的女人 提交于 2020-05-08 14:54:05

问题


I included this at the top of my php file:

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

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

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.

The collation my database has is "utf8_spanish_ci"

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>

I also tried:

<meta charset="utf-8">

and:

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

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?


回答1:


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.

Connection

  • 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");

Database

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

    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;

File-encoding

  • 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.

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.




回答2:


Are you sure? And are you sure that you are retrieving your data from the data base? Having said that, most databases require you to save data in a way that is NOT exactly like your question. There is really valid security reasons for this.




回答3:


You should use utf8_general_ci as a database encoding also before insert query you should run this query

Mysql_query(" SET NAMES 'utf8'");


来源:https://stackoverflow.com/questions/33948902/why-does-my-html-not-display-special-characters-taken-from-my-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!