LXXIV. Microsoft SQL Server and Sybase Functions (PDO_DBLIB)

简介

警告

本扩展模块是实验性的。本模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。使用本扩展模块风险自担。

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.

On Windows, you should 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.

目录
PDO_DBLIB DSN -- Connecting to Microsoft SQL Server and Sybase databases

add a note add a note User Contributed Notes
Steve H
01-Jun-2006 12:24
To connect to SQL Server 2005 Express on Windows, do it like this:

$pdo = new PDO ('mssql:host=localhost,1433;dbname=[redacted]', '[redacted]', '[redacted]');

localhost
localhost\SQLEXPRESS
localhost\1433
localhost:1433

will not work on Windows.

localhost,1433

does work.

YMMV on other OS's; try each.

Also make sure your TCP/IP Properties -> IP Addresses are correct under SQL Server Configuration Manager.
support at converters dot ru
25-Jan-2006 09:48
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.
graham1 dot simpson at hsbcib dot com
08-Sep-2005 03:19
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.