Reputation: 167
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
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()
Whether there is a problem in your database and tables Or in your PHP script.
CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
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;
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");
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
e.g :
$_GET
,$_POST
You can use mb_convert_encoding()
for convert your strings to UTF-8.
Useful Links :
PHP inserting Japanese string to utf8 table as something else, but still reads it successfully
https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434
Upvotes: 2