Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Introduction
This extension is EXPERIMENTAL. The behaviour of this extension including the names of its functions and any other documentation surrounding this extension may change without notice in a future release of PHP. This extension should be used at your own risk.
This extension is not available anymore on Windows with PHP 5.3 or later.
On Windows, you should use SqlSrv, an alternative driver for MS SQL is available from Microsoft: » http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx .
If it is not possible to use SqlSrv, you can use the PDO_ODBC driver to connect to Microsoft SQL Server and Sybase databases, as the native Windows DB-LIB is ancient, thread un-safe and no longer supported by Microsoft.
Table of Contents
- PDO_DBLIB DSN — Connecting to Microsoft SQL Server and Sybase databases
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Уровни абстракции
- Объекты данных PHP
- CUBRID Functions (PDO_CUBRID)
- Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
- Firebird Functions (PDO_FIREBIRD)
- IBM Functions (PDO_IBM)
- Informix Functions (PDO_INFORMIX)
- MySQL Functions (PDO_MYSQL)
- Microsoft SQL Server Functions (PDO_SQLSRV)
- Oracle Functions (PDO_OCI)
- ODBC and DB2 Functions (PDO_ODBC)
- PostgreSQL Functions (PDO_PGSQL)
- SQLite Functions (PDO_SQLITE)
- 4D Functions (PDO_4D)
Коментарии
There is currently little sybase related PDO docs out there. The ones that I found often mention a spec for a dsn that is invalid. Here's how I am currently connecting to sybase ASE:
1. Compile up freetds http://www.freetds.org on top of open client;
2. Add the PDO and PD_DBLIB modules to php 5 as per the documentation; Note: I'm currently using the PDO-beta and PDO_DBLIB-beta;
3. Check mods installed ok using "pear list" and "php -m";
The documentation often says to use "sybase:" as your DSN, this doesn't work. Use "dblib:" instead. Here's an example:
<?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 "Failed to get DB handle: " . $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);
?>
Hope this helps.
If You work with MSSQL Server 7.0/2000/... under Windows and use non latin Encoding then better To use PDO_MSSQL until PDO_ODBC bugs will be fixed (MSSQL ext far more stable and usabe for PHP versions <=5.1.2).
If your MSSQL connection use strings in OEM encoding (cp866 for russian charset)
1. Run Microsoft Server/Client Network Utility on work PC and UNCHECK "DBLibrary options"/"Automatic ANSI to OEM conversion"
2. Restart Web server if needed.
For people with issues inserting UTF-8 / Unicode data using DBLIB, you can't do this natively - but you can workaround the problem by converting the data first.
e.g. inserting into a nvarchar column with collation Latin1_General_CI_AS
...
$res = $db->prepare($sql);
$res->bindValue(':value', iconv('UTF-8', 'ISO8859-1', $value);
...
Hi All, I'wrote a class to connect to MSSQL/Azure databases with Transaction support.
Hope this can help anyone!
<?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";
}
}
}
?>
FYI: PDO dblib module (pdo_dblib.so) was installed when I installed php-mssql in CentOS 7. I thought php-mssql would just include the soon to be deprecated mssql PHP functions but it also contains the PDO connector. After installing this I'm able to connect to our MSSQL 2014 DB via PDO!
Watch out!
If you use PDO SQLSRV on windows 7, using 32 bit php on XAMMP, you might encounter driver problems : "This extension requires the Microsoft ODBC Driver 11 for SQL Server to communicate with SQL Server"
The reason, Microsoft 32-bit ODBC driver doesn't install properly on 64-bit Windows 7.
Check the solution to PDO SQLSRV driver problem here in StackOverflow
https://stackoverflow.com/a/46245990/1330248
Instead of using Mssql or DBLib extension you should use the official extensions from Microsoft from here: https://github.com/Microsoft/msphpsql