-1

i am trying to store some info in my db. One of the fields are Japanese names. I am getting a error:

Warning: #1366 Incorrect string value: '\xE3\x83\xA9\xE3\x83\x87...' for column 'japan-name' at row 1

So i cannot chnage the charset of my db. Can i use PHP or Javascript to convert Japanese/Korean to something else, and them when i go read it, reconvert to Japanese/Korean?

2 Answers2

0

PHP offers the base64_encode() and base64_decode() functions. They are fast, and impose a storage penalty of about 33%. You can use the first to convert your utf-8 east Asian text to what looks like gibberish in ASCII before you store it in your table. The second will convert it back after you retrieve it.

Here's an example:

$jp = " 私はガラスを食べられます。それは私を傷つけません。";
$jpe = base64_encode ($jp);
$jpd = base64_decode ($jpe);

After you run these lines, the $jpe variable has the value

IOengeOBr+OCrOODqeOCueOCkumjn+OBueOCieOCjOOBvuOBmeOAguOBneOCjOOBr+engeOCkuWCt+OBpOOBkeOBvuOBm+OCk+OAgg==

That stores just fine in an ASCII or Latin-1 column.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
-2

utf-8 saves the unicode data in table... but other way is to encode and save and then decode and display

update: searched on web and found answer at How do you Encrypt and Decrypt a PHP String?

define("ENCRYPTION_KEY", "!@#$%^&*");
$string = "This is the original data string!";

echo $encrypted = encrypt($string, ENCRYPTION_KEY);
echo "<br />";
echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);

/**
 * Returns an encrypted & utf8-encoded
 */
function encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

/**
 * Returns decrypted original string
 */
function decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}
Community
  • 1
  • 1
Asad Nauman
  • 971
  • 11
  • 19
  • LOl is not correct you just writted what i asked. I need something to convert... some command...i dont need you to repeat what i asked LOOOL – William Philippe Jul 30 '15 at 12:20
  • So if you are not going to help me with some php command convert or javascript function i will not mark as correct. – William Philippe Jul 30 '15 at 12:21
  • 1
    Seriously? **Encryption** to solve a problem with charsets? Just no. – deceze Jul 30 '15 at 12:46
  • instead of giving negative point straight forward you should read my comments where user does not wanted to change the database type. and I have also commented on the same time that it is not recommended :( – Asad Nauman Jul 30 '15 at 17:11
  • Regardless of that, there are any number of simple **encodings** which could solve this problem, instead of a completely misplaced **encryption** algorithm. – deceze Jul 31 '15 at 05:59