Php连接mysql处理中文乱码

 SET character_set_client = utf8 ;
 SET character_set_connection = utf8 ;
 SET character_set_database = utf8 ;  
 SET character_set_results = utf8 ;
 SET character_set_server = utf8 ;
 SET collation_connection = utf8 ;  
 SET collation_database = utf8 ;  
 SET collation_server = utf8 ;

insert into forum(forumname,manager)values('你还','丰富的');

1. 中文,请确保 表中该字段的字符集为中文兼容:  big5     | Big5 Traditional Chinese  gb2312   | GB2312 Simplified Chinese  gbk      | GBK Simplified Chinese  utf8     | UTF-8 Unicode  

2. 确保,联接参数与这个字段字符集一致,你可以用 set name 'charsetname';  比如, set name 'gbk';  这条命令会同时修改 character_set_client,character_set_connection,character_set_results  (如果你的这架MySQL中都为中文,则你可以在my.ini或my.cnf中加上或修改这个参数, 参数文件修改后需重启MySQL服务) [mysql] default-character-set=gbk  

3. PHP 乱码, 同样 mysql_query("set name 'gbk'"); 其它API也类似。  

4. phpmyadmin里乱码 phpMyAdmin的config.inc.php中有没有设置$cfg['DefaultCharset']='utf-8';  

5. Windows操作系统中命令行("DOS"窗口)下。  在你的DOS窗中的左上角标题栏片左键,属性,  在字体中,选择“宋体”,确认  mysql中 set names 'gbk';  

6. ADO.NET, ADO中 ,可以连接字符串中加入CharSet=UTF8;类似指令以说明connection的字符集。  Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=UTF8;  

7. SQL Manager for MySQL 用EMS建数据库,  Character Set设为utf-8  client charset设UTF-8  Font charset 设为GB2312_CHARSET

8. jdbcodbc桥接 http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html        // Load the JDBC-ODBC bridge driver        Class.forName(sun.jdbc.odbc.JdbcOdbcDriver) ;

       // setup the properties        java.util.Properties prop = new java.util.Properties();        prop.put( " charSet " , " Big5 " );        prop.put( " user " , username);        prop.put( " password " , password);

       // Connect to the database        con = DriverManager.getConnection(url, prop);  

9.  PHP 5.2 版本以上解决乱码问题的一个方法 (由 ljf_ljf [Mark Liang] 提供)     $conn = mysql_connect ( " 192.168.1.133 " , " root " , " 123456 " ) or         die ( " Could not connect: " . mysql_error ());

    $program_char = " utf8 " ;

    $conn . mysql_select_db ( " test " );     // $conn.mysql_query('SET @@character_set_results = "'.$program_char.'"');         mysql_set_charset( $program_char , $conn );     $charset = mysql_client_encoding ( $conn );     printf ( " current character set is %s <br> " , $charset );     $result = mysql_query ( " SELECT id, task_no,pack_path FROM tb_workplan where id = 1 " , $conn );     while ( $row = mysql_fetch_array ( $result , MYSQL_BOTH)) {         printf ( " ID: %s <br> task_no: %s  <br> pack_path :%s <BR> " , $row [ " id " ] , $row [ 1 ] , $row [ " pack_path " ]);      }     $conn . mysql_free_result ( $result );     $conn . mysql_close ();  

10.  存储过程参数乱码 create procedure t ( aa char(10) charset 'gbk')

posted on 2013-04-28 14:54  xmdl  阅读(333)  评论(0编辑  收藏  举报

导航