1

how can I make my PHP script echo values from MySQL with proper encoding?

Currently, it returns as shown in a screenshot below while the database is set to coding also shown below.

How the value is shown

database structure

data stored

Kolombooo
  • 67
  • 4
  • Sounds like two problems. Plan "question mark" comes from one cause; "black diamond" comes from another. Both are discussed here: https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Jan 02 '22 at 06:17

2 Answers2

0

If you are using PDO, please specify the charset when you create the db connection as follows:

$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8_czech_ci", $user, $pass);

Whereas if you are using mysqli, please use the following :

$conn = mysqli_connect($servername, $username, $password, $dbname);

/* change character set to utf8_czech_ci */
mysqli_set_charset($conn,"utf8_czech_ci");
$mysqli->query("set names utf8_czech_ci");
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
0

There’s a few issues that can be causing this (went through the same bs myself)

Above works if it's just your database in the wrong default charset, you also have to set both PHP and HTML headers to the correct encoding.

header('Content-Type: text/html; charset=utf-8');
ini_set("default_charset", "UTF-8");
mb_internal_encoding("UTF-8");
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "UTF-8");

Can all affect it. Looks like the data is saving correctly, so it's just a presentation issue. Without more info it's hard to say if it's a php, html, or library issue but the above should pretty much nuke the issue.

Mason Stedman
  • 613
  • 5
  • 12