php MsSql server時(shí)遇到的中文編碼問(wèn)題
更新時(shí)間:2009年06月11日 02:52:03 作者:
朋友要用sugarcrm的php讀取Ms sql server的中文資料,因?yàn)槠湓假Y料是Access 數(shù)據(jù)庫(kù),導(dǎo)到mysql不太方便。
但導(dǎo)到sqlserver 2005后,發(fā)現(xiàn)其中文編碼只支持GB 和 UCS-2(unicode 16),所以直接在數(shù)據(jù)庫(kù)中查詢顯示正確,但使用php的utf9編碼顯示時(shí)則全是亂碼。
找了大量資料,什么使用mssql,freetds,odbc,ado或直接每次查詢和寫入都進(jìn)行轉(zhuǎn)碼等建議都有。不過(guò)實(shí)際測(cè)試中,發(fā)現(xiàn)Ado這種方法是好用的。
代碼如下:
<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");
//access 數(shù)據(jù)庫(kù)的打開(kāi)方式
//$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;") ;
// 執(zhí)行查詢并輸出數(shù)據(jù)
$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
// 釋放資源
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
</body>
</html>
查詢結(jié)果(與使用sql server managment studio效果一樣):
<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>
找了大量資料,什么使用mssql,freetds,odbc,ado或直接每次查詢和寫入都進(jìn)行轉(zhuǎn)碼等建議都有。不過(guò)實(shí)際測(cè)試中,發(fā)現(xiàn)Ado這種方法是好用的。
代碼如下:
復(fù)制代碼 代碼如下:
<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");
//access 數(shù)據(jù)庫(kù)的打開(kāi)方式
//$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;") ;
// 執(zhí)行查詢并輸出數(shù)據(jù)
$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
// 釋放資源
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
</body>
</html>
查詢結(jié)果(與使用sql server managment studio效果一樣):
ID | Title |
---|---|
114b0775-d9b2-db90-fcda-4a2f2cd7cdbd | 鏍紡浼?xì)绀炬湭鏉ュ晢浜?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 | 鏍紡浼?xì)绀炬湭鏉ュ晢浜?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效果一樣。
復(fù)制代碼 代碼如下:
<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>
查詢結(jié)果:
ID | Title |
---|---|
114b0775-d9b2-db90-fcda-4a2f2cd7cdbd | 株式會(huì)社未來(lái)商事 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 | 株式會(huì)社未來(lái)商事 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 |
因?yàn)閛dbc 也沒(méi)有設(shè)置內(nèi)碼頁(yè)的地方。
相關(guān)文章
PHP和JavaScrip分別獲取關(guān)聯(lián)數(shù)組的鍵值示例代碼
關(guān)聯(lián)數(shù)組的鍵值獲取,有很多方法,在本文為大家介紹下PHP和JavaScrip中時(shí)如何實(shí)現(xiàn)的,感興趣的朋友可以參考下2013-09-09php實(shí)現(xiàn)的微信紅包算法分析(非官方)
這篇文章主要介紹了php實(shí)現(xiàn)的微信紅包算法,以實(shí)例形式分析了拼手氣紅包的相關(guān)隨機(jī)算法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09