PHP not displaying some UTF-8 characters correctly

PHP is retriving data from MySQL database. Now it's suppose to display the results to screen, but since my database contains characters like (ä,ö,ü,õ), which PHP displays (�,䄔). Database displays all characters correctly, so I don't think the problem is there.

I add following line to my code, that sets charset to UTF-8.

$conn->set_charset("utf8");

After adding that line, PHP displays following characters „”.

Also tried:

$conn->query("SET character_set_results=utf8");
$conn->query("SET NAMES 'utf8'");

How can I get PHP to display correct characters?

Here is the important part of my code

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT name FROM employee";
$stmt = $conn->prepare($sql);
$stmt->execute();

$stmt->bind_result($employee);
while($stmt->fetch()){
    echo "$employee|";
}
$stmt->close();
$conn->close();

I'm running on PHP 7 and MySQL 10.1.19-MariaDB.

Upvotes: 1

Views: 2415

Answers (1)

nithinTa
nithinTa

Reputation: 1642

Consider the following things in this situations.

Make sure the header match the following

Content-Type:text/html; charset=UTF-8

and try setting UTF-8 in html

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

The source file encoding can make problems when string are hard coded in file. Since this file echoing it that seem to be no issue.

Upvotes: 1

Related Questions