Save Japanese characters into MySQL

I am using PHP for executing a MySQL update sentence with Spanish, English and Japanese characters.

But i'm not able to save Japanese characters into the database. How should I proceed?

Database has utf8_general_ci collation.

$strSQL = "UPDATE table SET value = '" . addslashes($strValue) . "'";
$strSQL = utf8_decode($strSQL);
mysqli_query($cnn, $strSQL);

With addslashes I can get apostrophes to be saved into the database.

With utf8_decode I can get Spanish characters to be saved into the database.

Upvotes: 0

Views: 5885

Answers (1)

Max Base
Max Base

Reputation: 641

Why you proccess utf8_decode() the sql query?

Look at http://php.net/manual/en/function.utf8-decode.php for details about utf8_decode()


You have to check a few things.

Whether there is a problem in your database and tables Or in your PHP script.  

 

Database :

CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Table Charset :

CREATE TABLE test.table
(
    id INT NOT NULL AUTO_INCREMENT,
    text VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = MyISAM;

Connection :

SET NAMES utf8;

E.g :

$mysqli = new mysqli("localhost","my_user","my_password","test_db");
$mysqli->set_charset("utf8");

OR

$link = mysqli_connect("localhost","my_user","my_password","test_db");
mysqli_set_charset($link,"utf8");

Configure the encoding charset of server :

With PHPMyAdmin , Choose UTF-8 when you login.

PHP Script :

header('Content-Type: text/html;charset=UTF-8');

Apache Config (/etc/httpd/conf/httpd.conf) :

AddDefaultCharset UTF-8

Apache .htaccess file :

AddCharset UTF-8 .htm
AddCharset UTF-8 .html
AddCharset UTF-8 .php

PHP Config (/etc/php.ini) :

default_charset = "utf-8"

MySQL Config (/etc/my.cnf ) :

[client]
default-character-set=utf8

[mysqld]
default-collation=utf8_unicode_ci
character-set-server=utf8
default-character-set=utf8
init-connect='SET NAMES utf8'
character-set-client = utf8

User values :

e.g : $_GET , $_POST

You can use mb_convert_encoding() for convert your strings to UTF-8.


Useful Links :

Upvotes: 2

Related Questions