栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Can't insert Chinese character into MySQL

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 pre is tested pre please do the following its works.

If you have already created database please ALTER it as below pre. And if you
didn’t created database then create it and set Collation to utf8_unipre_ci

And same for table define Collation as “utf8_unipre_ci” for table fields in
which you want to store chines chars.

ALTER DATAbase `stackoverflow` DEFAULT CHARACTER SET utf8 COLLATE utf8_unipre_ci;CREATE TABLE `stackoverflow`.`chines`(    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,    `words` VARCHAr( 200 ) CHARACTER SET utf8 COLLATE utf8_unipre_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.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421034.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号