1

Am trying to insert korean characters to mysql leveraging PHP Pdo and mysql. When I insert charcater 다시 말해 주세요 it shows ?? ?? ??? inside mysql database

Here is how I created table

create table base_tab (id int primary key auto_increment, content text, username varchar(20)) CHARACTER SET utf8 COLLATE utf8_unicode_ci;

here is the code

$db = new PDO ('mysql:host=localhost;dbname=mydb_test;charset=utf8', 
    'root', // username
    ''// password

);

$timer =  time();
$statement = $db->prepare('INSERT INTO base_tab
(content,username)
 
                          values
(:content,:username)');

$statement->execute(array( 

':content' => '다시 말해 주세요',
':username' => 'ann'
));


if($statement){

echo "success";
}else{

echo "failed";

}
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38

2 Answers2

0

you will need to enable CJK, and select one of the various options when creating your Database, Table or Columns.

Documentation

For Korean use

  1. euckr character set

For Japanese use

  1. sjis character set
  2. ujis character set
  3. cp932 character set

For Chinese use

  1. big5 character set

Show what the current settings are:

SHOW VARIABLES LIKE 'char%';
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
0

I tried to repeat your problem, so I went through the following steps but didn't face any issues:

1. Created a new database with the mydb_test name and utf8_general_ci character set:
enter image description here

2. Copied your code and executed, returned "Success" and inserted data just correctly enter image description here

So, I think the point is the database charset that needs to be utf8_general_ci upon creation :)

Edit: Change utf8_unicode_ci to utf8_general_ci when creating the table, it should be like this:

create table base_tab (id int primary key auto_increment, content text, username varchar(20)) CHARACTER SET utf8 COLLATE utf8_general_ci;
Mohandes
  • 452
  • 3
  • 15
  • Can you create a sample with my code @Mohandes – Nancy Moore Nov 21 '21 at 07:38
  • You mean 'Can I insert data with your code?', yes I can, your code just works fine, the problem is with the database charset. – Mohandes Nov 21 '21 at 07:43
  • I also edited the answer(changed utf8mb4 to utf8), please check :) – Mohandes Nov 21 '21 at 07:44
  • Please show me how you create your database with utf8mb4 or utf8 because my own is throwing error – Nancy Moore Nov 21 '21 at 07:48
  • I created that using phpmyadmin, you can also use phpmyadmin if you have access to it. – Mohandes Nov 21 '21 at 07:50
  • @Mohandes , You may find this helpful. [How to convert an entire MySQL database characterset and collation to UTF-8?](https://stackoverflow.com/questions/6115612/how-to-convert-an-entire-mysql-database-characterset-and-collation-to-utf-8) – steven7mwesigwa Nov 21 '21 at 17:23