Home  >  Article  >  Backend Development  >  PHP向MYSQL插入中文,有有关问题

PHP向MYSQL插入中文,有有关问题

WBOY
WBOYOriginal
2016-06-13 13:44:36736browse

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试试;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn