简体   繁体   中英

Migrate mySQL data from php5.2 to php7

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM