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
headerin php file. - Changed string encoding with
mb_convert_encoding. - Set
default_charsetinphp.inito 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
http://mysql.rjweb.org/doc.php/charcoll#php
More on Mojibake: Trouble with UTF-8 characters; what I see is not what I stored