1

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.

3

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you can also call header('Content-Type: text/html; charset=UTF-8'); at the very beginning or your code
I did try that solution at that time, but found out that problem was in java client side program. Anyway still good solution, maybe it'll help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.