php MsSql server时遇到的中文编码问题
但导到sqlserver 2005后,发现其中文编码只支持GB 和 UCS-2(unicode 16),所以直接在数据库中查询显示正确,但使用php的utf9编码显示时则全是乱码。
找了大量资料,什么使用mssql,freetds,odbc,ado或直接每次查询和写入都进行转码等建议都有。不过实际测试中,发现Ado这种方法是好用的。
代码如下:
复制代码 代码如下:
//print("The next line generates an error.
");
//printaline("PLEASE?");
//print("This will not be displayed due to the above error.");
?>
$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('SELECT * FROM accounts') or die ("error query");
?>
ID | Title |
---|---|
'. $rs->Fields['id']->Value .' | ';'. $rs->Fields['name']->Value .' | ';
// 释放资源
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
查询结果(与使用sql server managment studio效果一样):
ID | Title |
---|---|
114b0775-d9b2-db90-fcda-4a2f2cd7cdbd | 鏍紡浼氱ぞ鏈潵鍟嗕簨 629487 |
1d270085-a588-9ea7-584c-4a2f2c8d1a5b | Fabriqu茅 Interation氓l 79436 |
23 | 中文 |
36ea2575-fe34-61b0-e5ae-4a2f2c791d22 | Berufskolleg f眉r Elektrotechnik 65790 |
3834261a-fd48-9d4a-be40-4a2f2c5fc256 | Berufskolleg f眉r Elektrotechnik 529523 |
52c9652c-82c8-ec2b-72ae-4a2f2c3a58d6 | 鏍紡浼氱ぞ鏈潵鍟嗕簨 42138 |
78931a0e-f582-f406-8a56-4a2f2c3741b0 | But茅e Torique 700010 |
989473f7-6b7b-fed3-12a1-4a2f2c320645 | A.B.銈便偄銉栥儸銈ゃ兂銈?181212 |
b4579151-55cb-5ae4-a5f1-4a2f2c173b18 | B眉nde-Mitte 203765 |
d72c42c9-9e1d-b926-d931-4a2f2c2a3100 | Berufskolleg f眉r Elektrotechnik 27682 |
e97002f1-035f-91d3-4592-4a2f2f780e01 | zhh |
其中,23 ID的编码是GB2312的,其余是utf-8的。
而使用odbc则跟使用mssql_connect效果一样。
复制代码 代码如下:
//print("The next line generates an error.
");
//printaline("PLEASE?");
//print("This will not be displayed due to the above error.");
?>
//$conn = odbc_pconnect("myodbc", "sa", "cvttdev", 0);
//$connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath("mydb.mdb");
$connstr="Driver={SQL Server};Server={192.168.22.40};Database=sugarcrm_db;UID=sa;PWD=123456;";
$connid=odbc_connect($connstr,"sa","cvttdev",SQL_CUR_USE_ODBC );
$query=odbc_do($connid,"select id,name from accounts");
?>
ID | Title |
---|---|
'. $id .' | ';'. $name .' | ';
查询结果:
ID | Title |
---|---|
114b0775-d9b2-db90-fcda-4a2f2cd7cdbd | 株式会社未来商事 629487 |
1d270085-a588-9ea7-584c-4a2f2c8d1a5b | Fabriqué Interationål 79436 |
23 | ���� |
36ea2575-fe34-61b0-e5ae-4a2f2c791d22 | Berufskolleg für Elektrotechnik 65790 |
3834261a-fd48-9d4a-be40-4a2f2c5fc256 | Berufskolleg für Elektrotechnik 529523 |
52c9652c-82c8-ec2b-72ae-4a2f2c3a58d6 | 株式会社未来商事 42138 |
78931a0e-f582-f406-8a56-4a2f2c3741b0 | Butée Torique 700010 |
989473f7-6b7b-fed3-12a1-4a2f2c320645 | A.B.ケアブレイン��?181212 |
b4579151-55cb-5ae4-a5f1-4a2f2c173b18 | Bünde-Mitte 203765 |
d72c42c9-9e1d-b926-d931-4a2f2c2a3100 | Berufskolleg für Elektrotechnik 27682 |
e97002f1-035f-91d3-4592-4a2f2f780e01 | zhh |
因为odbc 也没有设置内码页的地方。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
