Home >Backend Development >PHP Tutorial >thinkphp3查询mssql数据库乱码解决方法分享_PHP

thinkphp3查询mssql数据库乱码解决方法分享_PHP

WBOY
WBOYOriginal
2016-06-01 11:57:16980browse

ThinkPHP

thinkphp查询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了

复制代码 代码如下:
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
  for($j=0;$j   //取查询结果编码改为UTF-8,并存入$Row,且$Row与$Result键与值一致                     
   $Row[$key1[$i]][$key2[$j]]=iconv('gb2312','utf-8',$Result[$key1[$i]][$key2[$j]]);
  }        
 }      
 retrun $Row; 
}

Statement:
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