Problem with diacritics

Discuss coding issues, and scripts related to PHP and MySQL.
mluci12
Posts: 39

Problem with diacritics

Hello!
I created a blog but I have problem with Romanian characters: autobelgia.ro/article.php?id=7

Admin Posts: 805
Hello
I see no problem with characters in that page. The romanian characters, diacritics, are displayed well.
Copied from that page:
mașină Începând cu data de 1 Mai 2016, Belgia a trecut la adoptarea placilor de tranzit în format european. Instituția
Anyway, if you have problems with diacritics or other less usual characters in your web page, follow these steps:
1. MySQL database must be created with this setting: utf8_general_ci
2. The tables in MySQL must be created having this instruction: CHARACTER SET utf8 COLLATE utf8_general_ci
3a. When it is created the connection with MySQLi, you must add the setting for UTF-8:

Code: Select all

$conn = new mysqli($host, $user, $password, $db_name);
$conn->query('SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8";');
3b. Or, if you use PDO:

Code: Select all

$conn = new PDO('mysql:host='. $host .'; dbname='. $db_name, $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec('SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8";');
4. In the php script you have to add header() function for UTF-8 (before displaying anything else, for example, after session_start()):

Code: Select all

header('Content-type: text/html; charset=utf-8');
5. The HTML page must have in the <head> area:

Code: Select all

<meta charset="utf-8" />
- If you follow these steps, the script should register and display well the diacritics and other less usual characters.