_php Techniques for Chinese coding problems encountered in PHP MSSQL server

Source: Internet
Author: User
Tags odbc mssql mssql server
However, after SQL Server 2005 was found, its Chinese encoding only supported GB and UCS-2 (Unicode 16), so the query is displayed correctly in the database, but the UTF9 encoding of PHP is all garbled when it is displayed.
Find a lot of information, what the use of Mssql,freetds,odbc,ado or direct each query and write a transcoding and other suggestions are available. However, the actual test, found that ADO this method is useful.
The code is as follows:
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<?php
Print ("The next line generates an error.<br>");
Printaline ("please?");
Print ("This won't be displayed due to the above error.");
?>

<?php

$conn = new COM ("ADODB. Connection ", NULL, Cp_utf8) or Die (" Cannot start ADO ");
How Access databases are opened
$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};D atabase=sugarcrm_db; Uid=sa; pwd=123456; ");
Execute Query and output data
$rs = $conn->execute (' SELECT * from accounts ') or Die ("error Query");
?>
<table border= "1" >
<tr><th>ID</th><th>Title</th>
</tr>
<?php
while (! $rs->eof) {
Echo ' <tr> ';
Echo ' <td> '. $rs->fields[' id ']->value. ' </td> ';
Echo ' <td> '. $rs->fields[' name ']->value. ' </td> ';
Echo ' </tr> ';
$rs->movenext ();
}
?>
</table>
<?php
Releasing resources
$rs->close ();
$conn->close ();
$rs = null;
$conn = null;

?>
</body>

Query results (same as using SQL Server Managment Studio effect):
ID Title
114b0775-d9b2-db90-fcda-4a2f2cd7cdbd 鏍  Lyon innocent 氱 takezo link  潵 鍟 嗕 簨 629487
1d270085-a588-9ea7-584c-4a2f2c8d1a5b Fabriqu interation 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 鏍  Lyon innocent 氱 takezo link  潵 鍟 嗕 簨 42138
78931a0e-f582-f406-8a56-4a2f2c3741b0 But e torique 700010
989473f7-6b7b-fed3-12a1-4a2f2c320645 a.b. 銈 偄 銉 栥 儸 銈 ゃ 兂-銈? 181212
B4579151-55cb-5ae4-a5f1-4a2f2c173b18 B Eyebrow Nde-mitte 203765
d72c42c9-9e1d-b926-d931-4a2f2c2a3100 Berufskolleg F Eyebrow R Elektrotechnik 27682
E97002f1-035f-91d3-4592-4a2f2f780e01 Zhh

Among them, the ID of the encoding is GB2312, the rest is utf-8.

Using ODBC is the same as using the Mssql_connect effect.

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<?php
Print ("The next line generates an error.<br>");
Printaline ("please?");
Print ("This won't be displayed due to the above error.");
?>

<?php
$conn = Odbc_pconnect ("Myodbc", "sa", "Cvttdev", 0);
$connstr = "Driver=microsoft Access DRIVER (*.mdb);D bq=". Realpath ("Mydb.mdb");
$connstr = "Driver={sql Server}"; server={192.168.22.40};D atabase=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 of the corporation 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 Future business of the corporation 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

Because ODBC also has no place to set the internal 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.