Home  >  Article  >  php教程  >  使用php 5时MySQL返回乱码的解决办法

使用php 5时MySQL返回乱码的解决办法

WBOY
WBOYOriginal
2016-06-13 10:31:33771browse

在使用 php(做为现在的主流开发语言) 5 中,通过 MySQL(和PHP搭配之最佳组合) 查询得到的值全部成为 ???????,原来是字符集设错了。

我在安装 MySQL(和PHP搭配之最佳组合) 5 时,已经选择默认字符集为 gb2312,但还是返回乱码,解决的办法是在连接 MySQL(和PHP搭配之最佳组合) 服务器后,调用一个与所设字符集相同的 "set names X" 语句(X 为你所设的字符集)。下面是我的连接函数:

function sql_connect() {
 global $MySQL(和PHP搭配之最佳组合)_HOST, $MySQL(和PHP搭配之最佳组合)_USER, $MySQL(和PHP搭配之最佳组合)_PASSWORD, $MySQL(和PHP搭配之最佳组合)_DATABASE;

 $connection = @MySQL(和PHP搭配之最佳组合)_connect($MySQL(和PHP搭配之最佳组合)_HOST, $MySQL(和PHP搭配之最佳组合)_USER, $MySQL(和PHP搭配之最佳组合)_PASSWORD) or startUpError(

Could not connect to MySQL(和PHP搭配之最佳组合) database.

,Connect Error);
 if (!MySQL(和PHP搭配之最佳组合)_select_db($MySQL(和PHP搭配之最佳组合)_DATABASE))
 {
  includeinstall.php(做为现在的主流开发语言);
  doInstall();
  MySQL(和PHP搭配之最佳组合)_select_db($MySQL(和PHP搭配之最佳组合)_DATABASE) or startUpError(

Could not select database: . MySQL(和PHP搭配之最佳组合)_error().

, Connect Error);
 }
 sql_query(set names "gb2312");    //sql_query是一个自定义的执行查询函数,关键是这个语句
 return $connection;
}
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