本人初学php+mysql,安装了XAMPP for windows 1.7.3。写了一个简单页面(用的是mysqli_connect()和mysqli_query())向数据库里面写入信息,但是在phpMyAdmin里面显示成乱码,在MySQL命令行里面显示也是乱码。但是在我自己做的php页面从数据库里面读取同样的数据,却能够正常显示内容。字符集都改成了UTF-8。数据库的字符集是utf8-unicode_ci。请问这是怎么回事?谢谢!

解决方案 »

  1.   

    http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
    MySQL 中文显示乱码
      

  2.   


    mysql_client_encoding($Code)
      

  3.   

    [PHP手册]mysql_client_encoding
    (PHP 4 >= 4.3.0, PHP 5)mysql_client_encoding -- 返回字符集的名称
    说明
    int mysql_client_encoding ( [resource link_identifier] )
    mysql_client_encoding() 返回当前连接的默认字符集名称。 例子 1. mysql_client_encoding() 例子<?php
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    $charset = mysql_client_encoding($link);
    printf ("current character set is %s\n", $charset);
    ?>  以上例子将产生如下输出: current character set is latin1
     
     
    参见 mysql_real_escape_string()。  add a note User Contributed Notesromain at dardour dot com
    15-May-2006 11:56 
    I couldn't get any luck with all the stuff mentioned below, and despite having an unicode DB, and setting all my field to utf8_general_ci...
    After looking around, I found that this page: http://dev.mysql.com/tech-resources/articles/4.1/unicode.html was adding the fields with an extra info before each value:<?
    mysql_query("INSERT INTO table SET field = _utf8'value'");
    ?>
    Mind the "_utf8" before the field value, and outside of the quotes.This works for me wether in an Insert or an Update statement.No need here for a <? mysql_query("SET NAMES utf8"); ?> before each query, or to change anything in the config files (that was important since I don't have access to these). 
    zayfod at yahoo dot com
    21-Apr-2006 07:57 
    The right lines to put in /etc/my.cnf (or other MySQL options file) are:[client]
    init-command="SET NAMES utf8"Unfortuantely the PHP mysql_connect() function does not use MySQL options files so this is not a sollution for changing the default connection character set for mysqlclient library v4.1+.The only working sollution remains:mysql_query("SET NAMES utf8", $conn);(of course /ext/mysql/php_mysql.c can always be patched ;] ) 
    Wio at psitrax dot de
    17-Mar-2006 01:24 
    If you set the encoding in my.cnf like[mysqld]
    init_connect='SET NAMES utf8'note that the content of init_connect  is not executed for users that have the SUPER privilege - ie root! 
    vrbcik
    28-Nov-2005 01:26 
    I have had problems with encoding after export of tables (from hosting - via PhpMyAdmin) and import them to other machine (my notebook - via PhpMyAdmin too). In PhpMyAdmin the encoding of all data was shown correctly, not that good with the web pages (data pulled via php).
    The first point is indication, that the data was imported correctly, but php script has got other character set than MySql is sending.The script's character set is set in header: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">. MySql setting was latin1.Then following code helped me:<?php
    mysql_query("SET CHARACTER SET 'latin2'", $conn);
    ?> 
    info at jantichy dot cz
    31-Dec-2004 06:02 
    Notice the difference between following two SQL statements:SET NAMES 'charset_name'
    SET CHARACTER SET charset_nameFor more detail see
    http://dev.mysql.com/doc/mysql/en/Charset-connection.html 
    black at scene-si dot org
    20-Oct-2004 07:04 
    the above (as it seems to be vaguely indicated in the mysql manual) works only with mysql 4.1+ 
    stian at grytoyr dot net
    19-Oct-2004 09:09 
    If you experience weird problems, like some UTF-8 characters (the Unicode character &#x010D and a few others in my case) seemingly being changed to garbage by mysql_query, you may need to do something like this before your actual query:<?php
    mysql_query("SET NAMES 'utf8'", $conn);
    ?>Took me days to figure that one out...
      

  4.   

    安装mysql的时候字符集选择“中文字符集”;
    在 mysql_connect()成功之后,执行以下这句试试:mysql_query("SET NAMES 'GB2312'");.
    我是这么解决问题的。
      

  5.   

    貌似phpmyadmin里面有个字符集的配置文件,不同版本不太一样,应该是config.hph这类的名字,你改成你自己的字符后,phpmyadmin显示就应该正常了。
    MySQL里的数据库、表以及表的字段都是可以设置字符集的,在MySQL理看到的是乱码,而在程序里看到的是正常的,说明你在建表的时侯,字符集定义的跟程序里的不一样,这得修改表以及表字段的字符集设置,这样以后加的数据就能正常显示了,但之前的数据在数据库里看还可能是乱码。
    相关设置你Google下,应该都有的,我以前遇到过这类问题,都是这么解决的。仅仅是个人的一点建议,希望能帮到你,不过又有点担心会误导你,呵呵,祝你好运!
      

  6.   

    建表的时候也要选择UTF8啊,在高级选项那里
      

  7.   

    运行程序时,先跑一句 mysql_query("set_names utf-8");说来也奇怪,我自从用了ci框架 后,gb还是utf-8都没乱码,快一年多了,都没深究这个事了。
      

  8.   

    数据库的字符集是utf8_general_ci 试试
      

  9.   

    是不是 utf8_general_ci  试一下 
      

  10.   

    需要同步结果集,查询,以及数据库 ,还有HTML的编码才可以