PhpMsSqlserver encountered Chinese encoding problems. However, after being imported to sqlserver2005, it is found that its Chinese encoding only supports GB and UCS-2 (unicode16), so the query in the database is displayed correctly, however, when php utf9 encoding is used, it is all messy, but after being imported to sqlserver 2005, it is found that its Chinese encoding only supports GB and UCS-2 (unicode 16 ), therefore, the query in the database is correct, but the utf9 encoding in php is garbled.
I have found a lot of information, and I suggest using mssql, freetds, odbc, ado or transcoding for each query and write. However, in actual tests, we found that Ado is a good method.
The code is as follows:
The code is as follows:
// 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 ");
// How to open the access database
// $ 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 ;");
// Execute the query and output data
$ Rs = $ conn-> Execute ('select * FROM accounts') or die ("error query ");
?>
| ID |
Title |
While (! $ Rs-> EOF ){Echo'
';Echo'
| '. $ Rs-> Fields ['id']-> Value .' | ';Echo'
'. $ Rs-> Fields ['name']-> Value .' | ';Echo'
';$ Rs-> MoveNext ();}?>
// Release resources
$ Rs-> Close ();
$ Conn-> Close ();
$ Rs = null;
$ Conn = null;
?>
Query Results (same as using SQL server Management studio ):
| ID |
Title |
| 114b0775-d9b2-db90-fcda-4a2f2cd7cdbd |
Please refer to the following link for more information: 629487 |
| 1d270085-a588-9ea7-584c-4a2f2c8d1a5b |
Fabriqu Mao Interation limit l 79436 |
| 23 |
Chinese |
| 36ea2575-fe34-61b0-e5ae-4a2f2c791d22 |
Berufskolleg f eyebrow r Elektrotechnik 65790 |
| 3834261a-fd48-9d4a-be40-4a2f2c5fc256 |
Berufskolleg f eyebrow r Elektrotechnik 529523 |
| 52c9652c-82c8-ec2b-72ae-4a2f2c3a58d6 |
Please refer to the following link for more information: 42138 |
| 78931a0e-f582-f406-8a56-4a2f2c3741b0 |
But Mao e Torique 700010 |
| 989473f7-6b7b-fed3-12a1-4a2f2c320645 |
A. B. Why does it happen? 181212 |
| B4579151-55cb-5ae4-a5f1-4a2f2c173b18 |
B Mei nde-Mitte 203765 |
| D72c42c9-9e1d-b926-d931-4a2f2c2a3100 |
Berufskolleg f eyebrow r Elektrotechnik 27682 |
| E97002f1-035f-91d3-4592-4a2f2f780e01 |
Zhh |
Among them, the 23id encoding is GB2312, and the rest are UTF-8.
Odbc is the same as mssql_connect.
The code is as follows:
// 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 |
While (odbc_fetch_row ($ query )){Echo'
';$ Name = odbc_result ($ query, 2 );$ Id = odbc_result ($ query, 1 );Echo'
| '. $ Id .' | ';Echo'
'. $ Name .' | ';Echo'
';}?>
Query Results:
| ID |
Title |
| 114b0775-d9b2-db90-fcda-4a2f2cd7cdbd |
Future business 629487 |
| 1d270085-a588-9ea7-584c-4a2f2c8d1a5b |
Fabriqué Interation limit 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 |
Future business 42138 |
| 78931a0e-f582-f406-8a56-4a2f2c3741b0 |
But é e Torique 700010 |
| 989473f7-6b7b-fed3-12a1-4a2f2c320645 |
A. B. why? 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 |
Because odbc does not set the inner code page.
After encoding 2005, it was found that its Chinese encoding only supports GB and UCS-2 (unicode 16), so the query directly in the database shows correct, however, when php utf9 encoding is used for display, it is all messy...