search
Homephp教程php手册php查询mssql出现问号(中文乱码)解决方法

在php连接mssql时查询出来的全部是问题了,这种问题我根据经验知道是编码问题,下面来给各位总结一下解决方法.

方法一,修改php.ini文件,当然根据你页面情况来设置也可以是utf-8编码了,代码如下:

;mssql.charset = "ISO-8859-1"

mssql.charset = "GBK"

方法二,直接程序中转换,代码如下:

iconv('GB2312','UTF-8',$data)

方法三,利用Ado连接在连接时设置编码,代码如下:

$conn = new COM("ADODB.Connection", NULL, CP_UTF8) or die("Cannot start ADO");

PHP例子,代码如下:

<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8″>   
</head>   
<body>   
<?php
//print("The next line generates an error.www.phprm.com<br>");
//printaline("PLEASE?");
//print("This will not be displayed due to the above error.");
?>   
<?php
$conn = new COM("ADODB.Connection", NULL, CP_UTF8) or die("Cannot start ADO");
//access 数据库的打开方式
//$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db");
//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
$conn->Open("Driver={SQL Server};Server={192.168.22.40};Database=sugarcrm_db;UID=sa;PWD=123456;");
// 执行查询并输出数据
$rs = $conn->Execute(&#39;SELECT * FROM accounts&#39;) or die("error query");
?>   
<table border="1″>   
<tr><th>ID</th><th>Title</th>   
</tr>   
<?php
while (!$rs->EOF) {
    echo &#39;<tr>&#39;;
    echo &#39;<td>&#39; . $rs->Fields[&#39;id&#39;]->Value . &#39;</td>&#39;;
    echo &#39;<td>&#39; . $rs->Fields[&#39;name&#39;]->Value . &#39;</td>&#39;;
    echo &#39;</tr>&#39;;
    $rs->MoveNext();
}
?>   
</table>   
<?php
// 释放资源
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>   
</body>   
</html>

总结:

一是:数据库类型,其中包括,数据库,表,字段三处都要统一,可以检查一下

二是:文件的编码类型,你若用dw或editplus可以查看页面编码,不同需修改

三是:访问数据库时的设置既set NAMES utf8;

四是:浏览器显示方式,添加meta属性


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment