0

We have a really old codebase with php5.2 some of them even in php3 using mysql 5.1 that we are trying to migrate to laravel 7/8. The data stored in the database is Japanese characters stored using latin1 encoding as,

  • ¥í¥°¥¤¥óÀ®¸ù
  • ¥í¥°¥¢¥¦¥È
  • ¥á¡¼¥ë¥¢¥É¥ì¥¹Êѹ¹

Those data are displayed correctly when using php5.2 and are working fine in the current codebase but when I try to access that data using any version beyond php5.2 I cannot get the correct value.

Things I tried but didn't work.

  • Changed the file encoding with header in php file.
  • Changed string encoding with mb_convert_encoding.
  • Set default_charset in php.ini to empty string.

But none of the solutions seems to work. Is there any other way I can correctly display those data?

$dsn = 'mysql:dbname=dbname;host=127.0.0.1;port=3306';
$user = 'root';
$password = '';
$db = new PDO($dsn, $user, $password);
$query = $db->prepare('SELECT * FROM tablename');
if ($query->execute()) {
    echo '<ul>';
    while ($row = $query->fetch()) {
        echo '<li>' . $row['column_name'] . '</li>';
    }
    echo '</ul>';
}

The same block of code displays correct data in the browser using php5.2 but it doesn't work in php7.3, how is that possible?

Lalit Thapa
  • 311
  • 5
  • 20
  • Could [this info](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) possibly help? There's quite a bit involved. – Paul T. Feb 12 '21 at 04:12
  • The problem is, I already have existing data that I need to convert in order for them to work with newer versions of `PHP` instead of setting up a completely new environment. – Lalit Thapa Feb 12 '21 at 04:22
  • @LalitThapa How do you think about creating a new database with `utf8mb4` ạnd importing the existing data? – Thân LƯƠNG Đình Feb 12 '21 at 05:33
  • Yeah, I already tried that but same problem with newer versions of PHP. – Lalit Thapa Feb 12 '21 at 06:15

1 Answers1

0

Specify the charset in the dsn, something like

$db = new PDO('dblib:host=host;dbname=db;charset=utf8mb4', $user, $pwd);

http://mysql.rjweb.org/doc.php/charcoll#php

More on Mojibake: Trouble with UTF-8 characters; what I see is not what I stored

Rick James
  • 135,179
  • 13
  • 127
  • 222