php – 无法在MySQL中插入中文字符

前端之家收集整理的这篇文章主要介绍了php – 无法在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);

Read More

And More

详细代码

下面代码是测试代码,请执行以下工作.

如果您已经创建了数据库,请将其更改为以下代码.
如果您没有创建数据库,则创建它并将Collat​​ion设置为utf8_unicode_ci

对于表格,将collat​​ion定义为“utf8_unicode_ci”,用于存储中文字符的表字段.

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>

查看结果和步骤,如下所示.

原文链接:https://www.f2er.com/php/135994.html

猜你在找的PHP相关文章