PHP Conference Japan 2024

Microsoft SQL Server 和 Sybase 函式 (PDO_DBLIB)

簡介

PDO_DBLIB 是一個驅動程式,它實作了 PHP 資料物件 (PDO) 介面,以便透過 FreeTDS 函式庫讓 PHP 能夠存取 Microsoft SQL Server 和 Sybase 資料庫。

此擴充功能在 Windows 上已不再可用。

在 Windows 上,您應該使用 SqlSrv,這是 Microsoft 提供的另一個 MS SQL 驅動程式:» http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

如果無法使用 SqlSrv,您可以使用 PDO_ODBC 驅動程式連線到 Microsoft SQL Server 和 Sybase 資料庫,因為原生 Windows DB-LIB 已經過時、非執行緒安全,且不再受 Microsoft 支援。

目錄

  • PDO_DBLIB DSN — 連線到 Microsoft SQL Server 和 Sybase 資料庫
新增註記

使用者貢獻的註記 7 則註記

Johankasselman at live dot com
9 年前
大家好,我寫了一個類別來連線到 MSSQL/Azure 資料庫,並支援交易功能。

希望這可以幫助到大家!

<?php
/**
* @author Johan Kasselman <johankasselman@live.com>
* @since 2015-09-28 V1
*
*/

class pdo_dblib_mssql{

private
$db;
private
$cTransID;
private
$childTrans = array();

public function
__construct($hostname, $port, $dbname, $username, $pwd){

$this->hostname = $hostname;
$this->port = $port;
$this->dbname = $dbname;
$this->username = $username;
$this->pwd = $pwd;

$this->connect();

}

public function
beginTransaction(){

$cAlphanum = "AaBbCc0Dd1EeF2fG3gH4hI5iJ6jK7kLlM8mN9nOoPpQqRrSsTtUuVvWwXxYyZz";
$this->cTransID = "T".substr(str_shuffle($cAlphanum), 0, 7);

array_unshift($this->childTrans, $this->cTransID);

$stmt = $this->db->prepare("BEGIN TRAN [$this->cTransID];");
return
$stmt->execute();

}

public function
rollBack(){

while(
count($this->childTrans) > 0){
$cTmp = array_shift($this->childTrans);
$stmt = $this->db->prepare("ROLLBACK TRAN [$cTmp];");
$stmt->execute();
}

return
$stmt;
}

public function
commit(){

while(
count($this->childTrans) > 0){
$cTmp = array_shift($this->childTrans);
$stmt = $this->db->prepare("COMMIT TRAN [$cTmp];");
$stmt->execute();
}

return
$stmt;
}

public function
close(){
$this->db = null;
}

public function
connect(){

try {
$this->db = new PDO ("dblib:host=$this->hostname:$this->port;dbname=$this->dbname", "$this->username", "$this->pwd");



} catch (
PDOException $e) {
$this->logsys .= "Failed to get DB handle: " . $e->getMessage() . "\n";
}

}

}

?>
Fabian G
6 年前
您應該使用 Microsoft 官方提供的擴充套件,而不是使用 Mssql 或 DBLib 擴充套件,網址如下:https://github.com/Microsoft/msphpsql
graham1 dot simpson at hsbcib dot com
19 年前
目前關於 Sybase 相關的 PDO 文件很少。我找到的那些文件經常提到一個無效的 DSN 規格。以下是目前我連線到 Sybase ASE 的方法

1. 在 Open Client 上編譯 freetds http://www.freetds.org
2. 根據文件將 PDO 和 PD_DBLIB 模組新增到 PHP 5;注意:我目前使用的是 PDO-beta 和 PDO_DBLIB-beta;
3. 使用「pear list」和「php -m」檢查模組是否已正確安裝;

文件經常說使用「sybase:」作為您的 DSN,但這無效。請改用「dblib:」。以下是一個範例

<?php
try {
$hostname = "myhost";
$port = 10060;
$dbname = "tempdb";
$username = "dbuser";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (
PDOException $e) {
echo
"無法取得資料庫連線: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while (
$row = $stmt->fetch()) {
print_r($row);
}
unset(
$dbh); unset($stmt);
?>

希望這個有幫助。
support at converters dot ru
18 年前
如果您在 Windows 下使用 MSSQL Server 7.0/2000/... 且使用非拉丁語系編碼,那麼最好使用 PDO_MSSQL,直到 PDO_ODBC 的錯誤被修復為止(MSSQL 擴充功能對於 PHP 版本 <=5.1.2 來說更穩定且可用)。
如果您的 MSSQL 連線使用 OEM 編碼的字串(俄羅斯字元集為 cp866)

1. 在工作電腦上執行 Microsoft 伺服器/用戶端網路公用程式,並取消勾選「DBLibrary 選項」/「自動 ANSI 轉換為 OEM」。

2. 如果需要,請重新啟動 Web 伺服器。
Vic L
7 年前
僅供參考:當我在 CentOS 7 中安裝 php-mssql 時,也安裝了 PDO dblib 模組 (pdo_dblib.so)。我以為 php-mssql 只會包含即將棄用的 mssql PHP 函式,但它也包含 PDO 連接器。安裝這個之後,我就能夠透過 PDO 連線到我們的 MSSQL 2014 資料庫了!
fleduc dot perso at gmail dot com
7 年前
注意!

如果您在 Windows 7 上使用 PDO SQLSRV,在 XAMMP 上使用 32 位元 PHP,您可能會遇到驅動程式問題:「此擴充功能需要 Microsoft ODBC Driver 11 for SQL Server 才能與 SQL Server 進行通訊」。

原因是 Microsoft 32 位元 ODBC 驅動程式無法在 64 位元 Windows 7 上正確安裝。

在 StackOverflow 中查看 PDO SQLSRV 驅動程式問題的解決方案

https://stackoverflow.com/a/46245990/1330248
ian at helastel dot com
12 年前
對於使用 DBLIB 插入 UTF-8 / Unicode 資料遇到問題的人來說,您無法直接執行此操作,但您可以透過先轉換資料來解決此問題。

例如,插入一個排序規則為 Latin1_General_CI_AS 的 nvarchar 欄位

...
$res = $db->prepare($sql);
$res->bindValue(':value', iconv('UTF-8', 'ISO8859-1', $value));
...
To Top