用php向Mysql数据库插入中文数据时数据库中显示乱码
PHP中文网
PHP中文网 2017-04-11 10:16:34
0
5
590

1.我写了php代码向mysql数据库插入一个带有中文的字段值,在phpMyadmin查看显示乱码,在mysql控制台查看也是乱码,我已经把每个字段的排序规则改为uft8_general_ci,在phpmyadmin里面直接插入显示正常,在mysql控制台直接插入也显示正常,只是通过php插入就不正常,我也在php代码写了header("Content-Type:text/html;charset=utf-8");

3.请大神帮看下问题出在哪里?

PHP中文网
PHP中文网

认证0级讲师

reply all (4)
黄舟

看你的数据库表里面有正常的中文,那就试插入数据的时候出现的问题。统一字符编码格式:

  • 确保数据库,表,字段设置的都是utf-8

  • 确保连接数据库的时候设置字符集是utf-8

  • 确保程序文件是utf-8

  • 确保mysql安装程序的配置文件设置字符utf-8

    小葫芦

    设置字符集试试

    mysqli_set_charset($con, "utf8");
      左手右手慢动作

      我找到问题关键了,下面两位回答的我都试过了,不起效果,问题出在mysql.ini配置少了一句话default-character-set = utf8 ,我是安装wampserver后用这个软件自己安装的mysql他的mysql.ini没有那句话,,而且如果加入这句话位置也不能乱加,必须加在我图片指出的位置

      用show variables like 'character_set_%;查看修改ini文件后的字符编码如下图

      • reply If you are using phpstudy and have a similar problem to the original poster, how should you solve it? When the SQL statement is written in PHP and run, Chinese garbled characters appear when browsing the data; when the SQL statement is run on the web page, the Chinese characters are also garbled.
        糖不甜 author 2018-08-31 18:23:46
      黄舟

      你要设置mysql编码 执行sql "set names utf8"

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!