Can't insert Chinese character into MySQL
Use UTF-8 when you create table.
create table table_name () CHARACTER SET = utf8;
Use UTF-8 when you insert to table
set username utf8; INSERT INTO table_name (ABC,VAL);
Read More
And More
Detailed Code
Below code is tested code please do the following its works.
If you have already created database please ALTER it as below code.
And if you didn't created database then create it and set Collation to utf8_unicode_ci
And same for table define Collation as "utf8_unicode_ci" for table fields in which you want to store chines chars.
ALTER DATABASE `stackoverflow` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE `stackoverflow`.`chines`
(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`words` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = InnoDB;
<?php
define('HOSTNAME', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'stackoverflow');
$dbLink = mysql_connect(HOSTNAME, USERNAME, PASSWORD)or die(mysql_error());
mysql_query("SET character_set_results=utf8", $dbLink)or die(mysql_error());
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db(DATABASE, $dbLink)or die(mysql_error());
mysql_query("set names 'utf8'",$dbLink)or die(mysql_error());
if(isset($_POST['addWord']))
{
mysql_query("SET character_set_client=utf8", $dbLink)or die(mysql_error());
mysql_query("SET character_set_connection=utf8", $dbLink)or die(mysql_error());
$sql_query = "INSERT INTO chines(words) VALUES('".$_POST['words']."')";
mysql_query($sql_query, $dbLink)or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
</head>
<body>
<?php
mysql_query("SET character_set_results=utf8", $dbLink);
$sql_query = "SELECT * FROM chines";
$dbResult = mysql_query( $sql_query, $dbLink)or die(mysql_error());
?>
<form action="" method="post">
<p>Word : <input type="text" name="words" /></p>
<p><input type="submit" name="addWord" /></p>
</form>
<?php
while($row = mysql_fetch_assoc($dbResult))
{
echo $row['words']. '<br />';
}
?>
</body>
</html>
See the result and step visual as below.
Unable to insert Chinese characters into MySQL db Despite creating table with utf8mb4/utf8mb4_general_ci
The following db/ table changes:
ALTER DATABASE <mydb> CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
ALTER TABLE <mytable> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
and changes in database.php:
...
'collation' => 'utf8mb4_unicode_520_ci',
...
Resolved the problem for me. php.ini already had the right setting.
Thanks @RickJames
problem when inserting chinese characters into mysql via C++ ODB
i double checked the visual studio settings of utf8, and found the default setting is UTF-8 with signature, changed it to utf-8 without signature makes the problem gone!
UnicodeEncodeError when inserting Chinese characters into mysql in python
Does that Python construct add quotes when doing the substitution? It needs to.
Did you establish utf8mb4 for the connection?
Is the table/column CHARACTER SET utf8mb4
?
More Python notes
I suggest utf8mb4 instead of utf8 because Chinese has some characters that need 4 bytes.
Related Topics
Test If Port Open and Forwarded Using PHP
How to Get Time Zone Through Ip Address in PHP
How to Delete $_Post Variable Upon Pressing 'Refresh' Button on Browser with PHP
PHP Namespace with Dynamic Class Name
How to Enable and Use Http Put and Delete with Apache2 and PHP
.Htaccess 301 Redirect of Single Page
Differencebetween Site_Url() and Base_Url()
Structure of a Serialized PHP String
Difference Between & and && in PHP
Php's Preg_Match() and Preg_Match_All() Functions
How to Test If a User Has Selected a File to Upload
Php, Get Tomorrows Date from Date
How Follow the Don't Repeat Yourself Principle When Consuming My Own Laravel API