search
HomeBackend DevelopmentPHP Tutorialphp MsSql server时遇到的中文编码问题_PHP

但导到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");
?>



while (!$rs->EOF) {
echo '';
echo '';
echo '';
echo '';
$rs->MoveNext();
}
?>
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");
?>



while(odbc_fetch_row($query))
{ echo '';
$name = odbc_result($query,2);
$id=odbc_result($query,1);
echo '';
echo '';
echo '';
}
?>
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 也没有设置内码页的地方。

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
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.