简体   繁体   中英

Special characters do not display in browser

When echoing data from the MySQL database I get strange symbols in the text. I've tried htmlspecialchars_decode() , but to no avail. The data is stored as a VARCHAR in the MySQL database and is displayed as they should when queried in MySQL workbench.

The characters include ' , ë , è , é , ê , ...

How do I get these characters to display in html?

屏幕截图

Have you declared a charset in the HTML of your page?

In HTML5 this is fine:

<meta charset="UTF-8">

Older (HTML 4.01) needs something like:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

You have encoding issues.

  1. You need to set <meta charset="UTF-8"> .

  2. You need to run query after you set up connection to DB "SET NAMES utf8"

  3. If you using PDO need set PDO like this new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword);

Also you can use this function which will encode accents to html entities

function convert($str){
     return mb_convert_encoding($str, "HTML-ENTITIES", "UTF-8");
}
echo convert($YourDataFromDB);

Hope it will helps

This worked for me:

$db = Database::getInstance();
$mysqli = $db->getConnection();
$mysqli->set_charset('utf8mb4'); 

Stolen from UTF-8 all the way through

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM