无法将汉字插入MySQL

无法将汉字插入MySQL

本文介绍了无法将汉字插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cookie使用big5集编码,并且无法插入MySQL。您能帮我解决这个问题吗?



字段:用户名是eng, date1 是日期, reason1 是中文字符。

  $ reason1 = $ _COOKIE [reason]; 
$ sql2 =INSERT INTO
attendance_count(username,date,count_time,appendix)
VALUES('$ username','$ date1','0','$ reason1') ;
mysql_query($ sql2);


解决方案

使用 UTF-8

  create table table_name()CHARACTER SET = utf8;  

在插入表格时使用 UTF-8

  set username utf8; INSERT INTO table_name(ABC,VAL);  




$ b

>以下代码是测试代码,请执行以下其工作。



如果您已经创建数据库,请将其更改为以下代码。
如果你没有创建数据库,然后创建并设置排序规则utf8_unicode_ci



和同为表定义排序规则为utf8_unicode_ci表字段中你想存储。中国字符

  ALTER DATABASE`stackoverflow`默认字符集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)或die(mysql_error());
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db(DATABASE,$ dbLink)或die(mysql_error());
mysql_query(set names'utf8',$ dbLink)或die(mysql_error());

如果(使用isset($ _ POST ['addWord']))
{
的mysql_query(SET character_set_client字符= UTF8,$ DBLINK)或死亡(mysql_error());
mysql_query(SET character_set_connection = utf8,$ dbLink)或die(mysql_error());

$ sql_query =INSERT INTO chines(words)VALUES('。$ _ POST ['words']。'');
mysql_query($ sql_query,$ dbLink)或die(mysql_error());
}

?>

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 dtd>
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =en>
< head>
< meta http-equiv =Content-Typecontent =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)或die(mysql_error());

?>

< form action =method =post>

< p> Word:< input type =textname =words/>< / p&
< p>< input type =submitname =addWord/>< / p>

< / form>
<?php
while($ row = mysql_fetch_assoc($ dbResult))
{
echo $ row ['words']。 '< br />';
}
?>
< / body>
< / html>

查看结果和步骤如下。



>


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);
解决方案

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.

这篇关于无法将汉字插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 23:45