日期:2014-05-17  浏览次数:20523 次

PHP向MYSQL插入中文,有问题
大家好,我是新手,在通过PHP向MYSQL插入中文是,就是插不进去啊。
我的代码如下,页面和MYSQL的字符集全是UTF-8的

$dbc = mysqli_connect('localhost', 'root', 'root', 'aliendatabase')
  or die('Error connecting to MySQL server.');

  $query = "INSERT INTO aliens_abduction (other) VALUES ('好')";
   
  $result = mysqli_query($dbc, $query)
  or die('Error querying database.');

  mysqli_close($dbc);

另外,在MYSQL的客户端插入汉字也有问题,报错如下

mysql> insert into a values('好');
ERROR 1366 (HY000): Incorrect string value: '\xBA\xC3' for column 'a' at row 1

MYSQL的字符集如下
mysql> show variables like '%char%';
+--------------------------+---------------------+
| Variable_name | Value |
+--------------------------+---------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------+


------解决方案--------------------
$query =

之前添加

mysql_query("SET NAMES 'UTF8'");
------解决方案--------------------
楼主正解!
你是dos窗口下执行的吧,如是的话,就在insert前执行下set names gbk试试;