Home > php教程 > PHP源码 > body text

解决thinkphp3查询mssql数据库乱码

PHP中文网
Release: 2016-05-25 17:00:01
Original
1046 people have browsed it

hinkphp查询mssql数据库出现乱码的原因是ThinkPHP默认为UTF-8,而msmsql数据库是简体中文版,存储的是GB2312编码
解决方法:
1:在ThinkPHP\Lib\Core 打开Db.class.php,在其最后面加上
2:在Db.class.php找到function select(),在$result = $this->query($sql);后面加一条 $result=iconv2utf8($result),就OK了

[PHP]代码    

public function iconv2utf8($Result) {        
	$Row=array();                   
	$key1=array_keys($Result);  //取查询结果$Result的数组的键值          
	//print_r($key1);          
	$key2=array_keys($Result[$key1[0]]);   
	//取查询结果$Result的第一个数组($key1[0])的键值           
	//print_r($key2);                  
	for($i=0;$i<count($key1);$i++) {  
		
		for($j=0;$j<count($key2);$j++) {                        
			//取查询结果编码改为UTF-8,并存入$Row,且$Row与$Result键与值一致                      
			$Row[$key1[$i]][$key2[$j]]=iconv(&#39;gb2312&#39;,&#39;utf-8&#39;,$Result[$key1[$i]][$key2[$j]]); 
		}         
	}       
	retrun $Row;  
}
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template