12

The cookie is encoded using the big5 set, and it cannot insert into MySQL. Could you help me to solve this problem?

Fields: username is eng, date1 is date, reason1 is a Chinese character.

$reason1 = $_COOKIE["reason"];
$sql2="INSERT INTO 
    attendance_count(username,date,count_time,appendix) 
    VALUES ('$username','$date1','0','$reason1')";
mysql_query($sql2);
4
  • What is the exact error message? use mysqli_error() to get it. Also it is better to use the new mysqli functions as the old mysql functions will be deprecated.
    – Tuim
    Commented Jan 22, 2013 at 10:28
  • 4
    SQL Injection read about it...
    – PeeHaa
    Commented Jan 22, 2013 at 10:31
  • Yeah, that website won't last an hour in the wild.
    – Niklas B.
    Commented Jan 22, 2013 at 10:36
  • I found the solution here forums.mysql.com/read.php?132,578240,579233
    – Phuong
    Commented May 9, 2016 at 13:00

6 Answers 6

30

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.

enter image description here

5
  • mysqli_query("SET character_set_results=utf8", $db); Commented Jan 22, 2013 at 10:45
  • or you can just do it by alter table field from phpmyadmin and set type to UTF-8...example .. ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8; Commented Jan 22, 2013 at 10:47
  • mysql_query("SET character_set_client=utf8", $dbLink); mysql_query("SET character_set_connection=utf8", $dbLink); Commented Jan 23, 2013 at 4:17
  • tanzilo.com/2008/10/13/… please follow every single step from this link it will help a lot in mean time i m writing code for same. Commented Jan 23, 2013 at 4:38
  • @DipeshParmar , Hi Dipesh , its working fine in mysql database successfully , i want to do the same concept in oracle database using php. what do i need to do?please help me on this.i have raised question please look at this link Commented Aug 24, 2016 at 4:26
5

For Chinese, you really need utf8mb4, not utf8. This is because of a small number of Chinese characters that require 4-byte UTF8 characters, which MySQL's utf8 does not support.

3
  • mysql says (1115, "Unknown character set: 'utf8mb4'"), mysql-v5.1.71 here.
    – CodyChan
    Commented Dec 7, 2016 at 7:26
  • 2
    Then you must update to at least 5.5. (5.5.3 is when utf8mb4 became available -- about a decade ago).
    – Rick James
    Commented Dec 7, 2016 at 19:14
  • a friend sent me this and saved me after hours of googling, thanks
    – Alex L
    Commented Jul 19, 2021 at 19:48
0

The real problem is about the cookie being encoded big5? Is the rest of the data in the client encoded as utf8? If 'yes' to both, ...

Change the connection to be big5, do the INSERT/SELECT of the cookie, then change back to utf8 (or, better, utf8mb4 after upgrading).

Executing SET NAMES big5 or calling the charset('big5') function for the API you are using is the preferred way to switch back and forth.

(Meanwhile, get away from the mysql_* interface; switch to mysqli_* or PDO.)

If everything in the client is big5, then there is no need to switch back and forth. Simply declare that the client is using big5. Note: It does not matter whether the table/column is also big5; MySQL will convert between them.

0

update character set of table columns as well with utf8 along with table character set. May be check in Mysql workbench alter table and see character set of columns you want to support Chinese characters.

0

for those who using c# connector in Visual Studio having difficulty insert chinese character into mysql database. (解决 MySQL 无法正常 insert 中文字题, 出现???乱码)

symptom ie. in C# query string

query = $@"INSERT INTO my_table SET name = '草泥马' ;"

after insert successful in mysql table show name = ???

Solution in mysql workbench

got edit table mode

  1. below tale name make sure table 'charset/collection' set to 'utf8mb4' and 'utf8mb4_0900_ai_ci'

  2. select the 'name' row, below column name choose 'charset/collection' and 'utf8mb4_0900_ai_ci' then hit Apply

back to visual studio.

  1. in c# connetion string add "Charset=utf8;" ie. String connectionString = "SERVER=your_serverip;Port=3306;DATABASE=my_database;UID=user;PASSWORD=pw;;Allow User Variables=True;SslMode=none;Charset=utf8";

it should work. hope it help you.

0

The collation for Chinese characters is gb18030_chinese_ci (Chinese, case-insensitive). You need to update the table with the above collation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.