php adodb連接mssql解決亂碼問題
更新時間:2009年06月12日 19:17:08 作者:
php程序是utf-8的,sqlserver是2005中文,內(nèi)碼是gb18030. 普通的mssql_connect無法設(shè)置內(nèi)碼轉(zhuǎn)換,讀出來的數(shù)據(jù)在utf-8頁面顯示亂碼。
周海漢/文
ADO可以用 new COM("ADODB.Connection", NULL, CP_UTF8)//65001 這樣的語句來實現(xiàn)正確轉(zhuǎn)換。但ADO對php的支持缺乏文檔。而有個開源的adodb,文檔較為豐富。
其中對不同數(shù)據(jù)庫驅(qū)動,設(shè)置UTF-8的方法還不一樣,如下:
For all drivers
'persist', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'dialect','charset','buffers','role'
M'soft ADO
'charpage'
MySQL
'clientflags'
MySQLi
'port', 'socket', 'clientflags'
Oci8
'nls_date_format','charset'
For all drivers
'persist', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'dialect','charset','buffers','role'
M'soft ADO
'charpage'
MySQL
'clientflags'
MySQLi
'port', 'socket', 'clientflags'
Oci8
'nls_date_format','charset'
其中,Ado可以使用charPage這個屬性來設(shè)置uft-8,類似new COM的方式。但發(fā)現(xiàn)當(dāng)將AdoNewConnection($dbdriver)的$dbdriver設(shè)為'ado'或'ado_mssql'時,其傳進(jìn)去的database被替換為provider。那database的名字如何設(shè)置呢?一直沒找到辦法。
$dbdriver='ado://sa:cvttdev@172.16.22.40/sqloledb?charpage=65001';
其格式是'driver://user:passwd@host/database?options[=value]
但沒解決設(shè)置數(shù)據(jù)庫名字的地方。
痛苦了很久,只能找到如下的辦法解決:
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<?php
$dbdriver='ado_mssql';
$server='192.168.22.40';
$user='sa';
$password='passwd';
$DATABASE='sugarcrm_db';
$database='sqloledb';
//$dbdriver='ado://sa:cvttdev@172.16.22.40/sqloledb?charpage=65001';
$myDSN="PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={172.16.22.40};DATABASE=sugarcrm_db;UID=sa;PWD=cvttdev;";
include('adodb5/adodb.inc.php');
$db = ADONewConnection($dbdriver); # eg 'mysql' or 'postgres'
$db->debug = true;
$db->charPage =65001;
//$db->Connect($server, $user, $password, $database);
$db->Connect($myDSN);
//error:mssql server not support codes below
//$db->Execute("set names 'utf8'");
echo "before query";
$rs = $db->Execute('select * from accounts');
print "<pre>";
print_r($rs->GetRows());
print "</pre>";
?>
</body>
</html>
ADO可以用 new COM("ADODB.Connection", NULL, CP_UTF8)//65001 這樣的語句來實現(xiàn)正確轉(zhuǎn)換。但ADO對php的支持缺乏文檔。而有個開源的adodb,文檔較為豐富。
其中對不同數(shù)據(jù)庫驅(qū)動,設(shè)置UTF-8的方法還不一樣,如下:
復(fù)制代碼 代碼如下:
For all drivers
'persist', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'dialect','charset','buffers','role'
M'soft ADO
'charpage'
MySQL
'clientflags'
MySQLi
'port', 'socket', 'clientflags'
Oci8
'nls_date_format','charset'
For all drivers
'persist', 'persistent', 'debug', 'fetchmode', 'new'
Interbase/Firebird
'dialect','charset','buffers','role'
M'soft ADO
'charpage'
MySQL
'clientflags'
MySQLi
'port', 'socket', 'clientflags'
Oci8
'nls_date_format','charset'
其中,Ado可以使用charPage這個屬性來設(shè)置uft-8,類似new COM的方式。但發(fā)現(xiàn)當(dāng)將AdoNewConnection($dbdriver)的$dbdriver設(shè)為'ado'或'ado_mssql'時,其傳進(jìn)去的database被替換為provider。那database的名字如何設(shè)置呢?一直沒找到辦法。
$dbdriver='ado://sa:cvttdev@172.16.22.40/sqloledb?charpage=65001';
其格式是'driver://user:passwd@host/database?options[=value]
但沒解決設(shè)置數(shù)據(jù)庫名字的地方。
痛苦了很久,只能找到如下的辦法解決:
復(fù)制代碼 代碼如下:
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<?php
$dbdriver='ado_mssql';
$server='192.168.22.40';
$user='sa';
$password='passwd';
$DATABASE='sugarcrm_db';
$database='sqloledb';
//$dbdriver='ado://sa:cvttdev@172.16.22.40/sqloledb?charpage=65001';
$myDSN="PROVIDER=MSDASQL;DRIVER={SQL Server};SERVER={172.16.22.40};DATABASE=sugarcrm_db;UID=sa;PWD=cvttdev;";
include('adodb5/adodb.inc.php');
$db = ADONewConnection($dbdriver); # eg 'mysql' or 'postgres'
$db->debug = true;
$db->charPage =65001;
//$db->Connect($server, $user, $password, $database);
$db->Connect($myDSN);
//error:mssql server not support codes below
//$db->Execute("set names 'utf8'");
echo "before query";
$rs = $db->Execute('select * from accounts');
print "<pre>";
print_r($rs->GetRows());
print "</pre>";
?>
</body>
</html>
相關(guān)文章
PHP讀取txt文件的內(nèi)容并賦值給數(shù)組的代碼
使用file_get_contents()獲取txt文件的內(nèi)容,然后通過explode()把獲得的字符串轉(zhuǎn)化為數(shù)組。獲得數(shù)組長度可以使用count()函數(shù)2011-11-11PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù))
本文主要對PHP的CURL方法curl_setopt()函數(shù)案例進(jìn)行介紹:1.抓取網(wǎng)頁的簡單案例;2.POST數(shù)據(jù)案例...下面就跟小編一起來看下吧2016-12-12在smarty中調(diào)用php內(nèi)置函數(shù)的方法
在smarty中調(diào)用php的內(nèi)置函數(shù),相信很多人都不是很很了解smarty的一個重要功能,就是能在smarty模板里面調(diào)用php的內(nèi)置函數(shù)2013-02-02PHP數(shù)據(jù)庫調(diào)用類調(diào)用實例(詳細(xì)注釋)
PHP開發(fā)中我們經(jīng)常需要用一些數(shù)據(jù)庫類,這里簡單的分享下調(diào)用類的代碼,學(xué)習(xí)php數(shù)據(jù)庫操作的朋友可以參考下2012-07-07php session_start()關(guān)于Cannot send session cache limiter - hea
在windows下編程,當(dāng)使用session_start()方法的時候,有時會報 session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/inpublisher/php1.php:1)這樣的錯誤2009-11-11