0

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.

Max Base
  • 639
  • 1
  • 7
  • 15
  • 1
    You absolutely should not be using `addslashes`, use `mysqli_real_escape_string`. You also should not use `utf8_decode`, you need to make sure you are using UTF-8 as your database and page encoding. You can find information on how to use UTF-8 in PHP online (phptherightway.com) – Andrea Dec 15 '18 at 16:20
  • If you still have trouble after implementing the changes suggested by Andrea and and Max Base, see if [_this_](https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored) helps. – Rick James Dec 15 '18 at 19:19

1 Answers1

2

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 :

Community
  • 1
  • 1
Max Base
  • 639
  • 1
  • 7
  • 15