Cara menggunakan mysql decode utf8

Correction to function converting utf82iso88592 and iso88592tutf8.
Janusz forgot about "ń", and "ż" exchanged from "ź" here and there.

GTo

function utf82iso88592($tekscik) {
     $tekscik = str_replace("\xC4\x85", "ą", $tekscik);
     $tekscik = str_replace("\xC4\x84", 'Ą', $tekscik);
     $tekscik = str_replace("\xC4\x87", 'ć', $tekscik);
     $tekscik = str_replace("\xC4\x86", 'Ć', $tekscik);
     $tekscik = str_replace("\xC4\x99", 'ę', $tekscik);
     $tekscik = str_replace("\xC4\x98", 'Ę', $tekscik);
     $tekscik = str_replace("\xC5\x82", 'ł', $tekscik);
     $tekscik = str_replace("\xC5\x81", 'Ł', $tekscik);
     $tekscik = str_replace("\xC5\x84", 'ń', $tekscik);
     $tekscik = str_replace("\xC5\x83", 'Ń', $tekscik);
     $tekscik = str_replace("\xC3\xB3", '?', $tekscik);
     $tekscik = str_replace("\xC3\x93", '?', $tekscik);
     $tekscik = str_replace("\xC5\x9B", 'ś', $tekscik);
     $tekscik = str_replace("\xC5\x9A", 'Ś', $tekscik);
     $tekscik = str_replace("\xC5\xBC", 'ż', $tekscik);
     $tekscik = str_replace("\xC5\xBB", 'Ż', $tekscik);
     $tekscik = str_replace("\xC5\xBA", 'ź', $tekscik);
     $tekscik = str_replace("\xC5\xB9", 'Ź', $tekscik);
     return $tekscik;
} // utf82iso88592

function iso885922utf8($tekscik) {
     $tekscik = str_replace("ą", "\xC4\x85", $tekscik);
     $tekscik = str_replace('Ą', "\xC4\x84", $tekscik);
     $tekscik = str_replace('ć', "\xC4\x87", $tekscik);
     $tekscik = str_replace('Ć', "\xC4\x86", $tekscik);
     $tekscik = str_replace('ę', "\xC4\x99", $tekscik);
     $tekscik = str_replace('Ę', "\xC4\x98", $tekscik);
     $tekscik = str_replace('ł', "\xC5\x82", $tekscik);
     $tekscik = str_replace('Ł', "\xC5\x81", $tekscik);
     $tekscik = str_replace('ń', "\xC5\x84", $tekscik);
     $tekscik = str_replace('Ń',"\xC5\x83", $tekscik);
     $tekscik = str_replace('?', "\xC3\xB3", $tekscik);
     $tekscik = str_replace('?', "\xC3\x93", $tekscik);
     $tekscik = str_replace('ś', "\xC5\x9B", $tekscik);
     $tekscik = str_replace('Ś', "\xC5\x9A", $tekscik);
     $tekscik = str_replace('ż', "\xC5\xBC", $tekscik);
     $tekscik = str_replace('Ż', "\xC5\xBB", $tekscik);
     $tekscik = str_replace('ź', "\xC5\xBA", $tekscik);
     $tekscik = str_replace('Ź', "\xC5\xB9", $tekscik);
     return $tekscik;
} // iso885922utf8

Once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. Indeed, navigating through UTF-8 related issues can be a frustrating and hair-pulling experience. This post provides a concise cookbook for addressing these issues when working with PHP and MySQL in particular, based on practical experience and lessons learned.

Share

Share

Cara menggunakan mysql decode utf8

Once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. Indeed, navigating through UTF-8 related issues can be a frustrating and hair-pulling experience. This post provides a concise cookbook for addressing these issues when working with PHP and MySQL in particular, based on practical experience and lessons learned.

Cara menggunakan mysql decode utf8

By Francisco Clariá

Verified Expert in Engineering

Francisco is an engineer focused on cross-platform apps (Ionic/Cordova) and specialized in hardware-software technology integration.

Klien mengalami masalah saat memindahkan database dari development server ke production server. Pada developmen server menggunakan penyortiran utf8mb4, sedangkan pada production hanya support utf8. Error pun terjadi ketika hendak impor sql di production server seperti pada gambar di atas. Kami sudah melakukan konversi secara manual, namun tidak berhasil. Lantas apa perbedaan antara utf8mb4 dan utf8?

utf8mb4 (disebut juga standard UTF-8) dapat menyimpan secara langsung suatu karakter yang ditentukan oleh Unicode, yang pertama adalah ukuran tetap pada 4 byte per karakter sedangkan yang terakhir adalah antara 1 dan 4 byte per karakter

utf8 hanya dapat menyimpan pertama 65.536 codepoints, yang akan mencakup CJVK (Cina, Jepang, Vietnam, Korea), dan menggunakan 1 sampai 3 byte per karakter.

Jadi yang perlu kami lakukan yakni dengan ALTER database beserta setiap tabel di dalamnya untuk mengubah set karakter (charset). Dengan menggunakan script PHP berikut proses konversi akan menjadi lebih mudah. Proses konversi database MySQL dari utf8mb4_unicode_ci ke utf8_general_ci berjalan dengan cepat.

<?php
$dbname = 'your-database-name';
mysql_connect('your-database-hostname', 'your-database-username', 'your-database-password');
mysql_query("ALTER DATABASE `$dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
$result = mysql_query("SHOW TABLES FROM `$dbname`");
while($row = mysql_fetch_row($result)) {
 $query = "ALTER TABLE {$dbname}.`{$row[0]}` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
 mysql_query($query);
 $query = "ALTER TABLE {$dbname}.`{$row[0]}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
 mysql_query($query);
}
echo 'All the tables have been converted successfully';
?>

  1. Copy PHP script di atas dan buat file dengan nama misalnya ‘dbconversion.php’.
  2. Upload file dbconversion.php ke server (development/production).
  3. Jalankan script dari ‘mydomain.com/dbconversion.php’.
  4. Setelah muncul pesan "All the tables have been converted successfully" maka proses konversi utfmb4 ke utf8 berjalan dengan baik.

Note:

Script di atas hanya berlaku untuk database mysql, jika Anda menggunakan mysqli maka cukup tambahkan parameter pada mysqli_query() menjadi: