yum安装版php5.3.0
+ mysql5.1.3(my.ini--->character_set_server=utf8)
编译安装php5.3.0
以上表示:两个不同的php环境,一个编译安装一个yum安装,版本相同,在使用mysql_connect()方法连接同一个mysql时,
在不设置mysql_query("set names utf")或mysql_set_charset("utf8")的前提下,它们默认使用的连库编码是不一样的
<?php $link=mysql_connect("localhost") or die(mysql_error()); $res=mysql_query("show variables like ‘%char%‘"); while($arr=mysql_fetch_assoc($res)){ var_dump($arr); }
yum版php
array ‘Variable_name‘ => string ‘character_set_client‘ (length=20) ‘Value‘ => string ‘latin1‘ (length=6) array ‘Variable_name‘ => string ‘character_set_connection‘ (length=24) ‘Value‘ => string ‘latin1‘ (length=6) array ‘Variable_name‘ => string ‘character_set_database‘ (length=22) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_filesystem‘ (length=24) ‘Value‘ => string ‘binary‘ (length=6) array ‘Variable_name‘ => string ‘character_set_results‘ (length=21) ‘Value‘ => string ‘latin1‘ (length=6) array ‘Variable_name‘ => string ‘character_set_server‘ (length=20) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_system‘ (length=20) ‘Value‘ => string ‘utf8‘ (length=4)
手动编译版php
array ‘Variable_name‘ => string ‘character_set_client‘ (length=20) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_connection‘ (length=24) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_database‘ (length=22) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_filesystem‘ (length=24) ‘Value‘ => string ‘binary‘ (length=6) array ‘Variable_name‘ => string ‘character_set_results‘ (length=21) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_server‘ (length=20) ‘Value‘ => string ‘utf8‘ (length=4) array ‘Variable_name‘ => string ‘character_set_system‘ (length=20) ‘Value‘ => string ‘utf8‘ (length=4)
通过phpinfo()查看两个不同php环境的编译参数,找到问题原因:
yum版的php中
--with-mysql=shared,/usr‘ ‘--with-mysqli=shared,/usr/lib64/mysql/mysql_config‘
这两个函数库默认调用的是MySQL客户端库(By default the MySQL database extensions are configured to use MySQL Client Library直译)的,这个时候Linux上是需要安装mysql,但是
编译版php
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
用的是mysqlnd(具体作用baidu,google)
两者最大的区别在于:mysqlnd连接数据库时,默认使用的是mysql服务器端show global variables like "%char%"显示的编码
而另一个始终是使用latin1做为连的库的编码,相当于无形中,一直设置了一个"set names latin1"