php MsSql server时遇到的中文编码问题
程序员文章站
2023-11-18 18:45:28
但导到sqlserver 2005后,发现其中文编码只支持gb 和 ucs-2(unicode 16),所以直接在数据库中查询显示正确,但使用php的utf9编码显示时则全...
但导到sqlserver 2005后,发现其中文编码只支持gb 和 ucs-2(unicode 16),所以直接在数据库中查询显示正确,但使用php的utf9编码显示时则全是乱码。
找了大量资料,什么使用mssql,freetds,odbc,ado或直接每次查询和写入都进行转码等建议都有。不过实际测试中,发现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 数据库的打开方式
//$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");
?>
<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>
查询结果(与使用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或直接每次查询和写入都进行转码等建议都有。不过实际测试中,发现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 数据库的打开方式
//$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");
?>
<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>
查询结果(与使用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效果一样。
复制代码 代码如下:
<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>
查询结果:
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 也没有设置内码页的地方。