Chinese Encoding Problems in php mssql Server

Source: Internet
Author: User

However, after importing to sqlserver 2005, it was found that its Chinese encoding only supports GB and UCS-2 (UNICODE 16), so the query directly in the database is displayed correctly, however, PHP utf9 encoding is used to display all garbled characters.
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.
Code As follows: Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? PHP
// Print ("the next line generates an error. <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 ");
// 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");
?>











while (! $ RS-> EOF) { echo ' '; echo ' '; echo ' '; echo ' '; $ RS-> movenext (); }< BR >?>
id title
'. $ RS-> fields ['id']-> value. ' '. $ RS-> fields ['name']-> value. '

// release resources
$ RS-> close ();
$ Conn-> close ();
$ rs = NULL;
$ conn = NULL;

?>
</Body>
</Html>

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.

Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? PHP
// Print ("the next line generates an error. <br> ");
// Printaline ("please? ");
// Print ("This will not be displayed due to the above error .");
?>

<? PHP
// $ 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 ");
?>
<Table border = "1">
<Tr> <TH> id </Th> <TH> title </Th>
</Tr>
<? PHP
While (odbc_fetch_row ($ query ))
{Echo '<tr> ';
$ Name = odbc_result ($ query, 2 );
$ Id = odbc_result ($ query, 1 );
Echo '<TD>'. $ id. '</TD> ';
Echo '<TD>'. $ name. '</TD> ';
Echo '</tr> ';
}
?>
</Table>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.