0

I'm encountering what I'm sure is an encoding error but I can't seem to find the weak link in my logic. In particular, when trying to insert a Greek lambda into my database, the cell is blank after the lambda.

The process:

Before any html is printed, I'm doing:

header('Content-Type: text/html; charset=utf-8');

Next, I'm connecting using PDO:

$dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'] . ';charset=utf8';
 $options = array(
            PDO::ATTR_EMULATE_PREPARES => false, 
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        try {
            $dbh = new PDO($dsn, $config['username'], $config['password'], $options);
            $dbh->exec("SET NAMES utf8");
            $dbh->exec("SET FOREIGN_KEY_CHECKS = 1");

When I do

SHOW FULL COLUMNS FROM my_table

I can see that the collation is utf8_general_ci. When I do:

echo mb_detect_encoding($data_to_be_entered);

I get UTF-8

When I echo out $data_to_be_entered into the console right before I insert it into the database, I see: (=23)

However, in my database, I just see ( as the entry.

Eric
  • 1,209
  • 1
  • 17
  • 34
  • 1
    You can have a look at [Cannot properly insert greek characters in mysql database](https://stackoverflow.com/questions/32782413/cannot-properly-insert-greek-characters-in-mysql-database) – Atif Oct 16 '19 at 17:58
  • See Truncation in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 17 '19 at 04:12
  • @Atif - Your reference talks about Mojibake; the problem here is Truncation. And it turns out not to be Greek. – Rick James Oct 17 '19 at 04:23

1 Answers1

0

That lambda is the Mathematical symbol. It is encoded in UTF-8 as the four bytes hex F09D9C86. Therefore, it needs MySQL CHARACTER SET utf8mb4, not utf8.

If you want the Greek lambda λ, that works fine with CHARACTER SET utf8 (or utf8mb4). The hex for it is CEBB.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Why did you reopen the question? The [dupe](https://stackoverflow.com/a/279279/285587) says exactly the same. – Your Common Sense Oct 17 '19 at 05:30
  • @YourCommonSense - (1) Truncation and Mojibake are different issues with different causes. (2) It was not a Greek letter that caused trouble, but a math symbol. (3) This needed utf8mb4; the other needed other solutions. – Rick James Oct 17 '19 at 15:21
  • @YourCommonSense - And... I struggled for years with UTF-8 issues; it took a long time to realize that there were at least 5 different causes of the problems, at least 4 different visible symptoms, and at least 6 different ways to fix them. Using the wrong fix (because of misdiagnosis, or because of assuming it is an exact dup) does the user a disservice. – Rick James Oct 17 '19 at 15:24