Microsoft SQL Server and Sybase Functions (PDO_DBLIB)

Introduction

Warning

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.

PDO_DBLIB is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to Microsoft SQL Server and Sybase databases through the FreeTDS libary.

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

Коментарии

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.
2005-09-08 03:19:01
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
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.
2006-01-25 07:48:10
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
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);
...
2012-07-17 18:27:26
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
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), 07);

       
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";
        }

    }

}

?>
2015-10-02 16:32:52
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
Автор:
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!
2017-02-23 19:59:15
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
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
2017-09-18 16:29:53
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html
Автор:
Instead of using Mssql or DBLib extension you should use the official extensions from Microsoft from here: https://github.com/Microsoft/msphpsql
2018-08-22 12:23:12
http://php5.kiev.ua/manual/ru/ref.pdo-dblib.html

    Поддержать сайт на родительском проекте КГБ