简体   繁体   English

使用 SQL Server 驱动程序通过 PDO 连接到 SQL Server

[英]Connect to SQL Server through PDO using SQL Server Driver

I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft .我正在尝试使用 PDO 和Microsoft 提供驱动程序连接到现有的 SQL Server 数据库。

I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?我看过使用 odbc、dblib、mssql 等的例子,但是我相信这些驱动程序的连接字符串应该使用 'sqlsrv'?

Are there any good examples of how to properly do this?是否有任何很好的例子来说明如何正确地做到这一点? If I should be doing this via some other method please let me know.如果我应该通过其他方法执行此操作,请告诉我。 Thanks!谢谢!

Well that's the best part about PDOs is that it's pretty easy to access any database.嗯,关于 PDO 的最好的部分是它很容易访问任何数据库。 Provided you have installed those drivers, you should be able to just do:如果您已经安装了这些驱动程序,您应该能够执行以下操作:

$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");

Mind you that in my experience and also of other ( PHP - Why is new SQLSRV driver slower than the old mssql driver? ) that using PDO_SQLSRV is way slower than through PDO_ODBC.请注意,根据我的经验以及其他( PHP - 为什么新的 SQLSRV 驱动程序比旧的 mssql 驱动程序慢? ),使用 PDO_SQLSRV 比通过 PDO_ODBC 慢得多。

If you want to use the faster PDO_ODBC you can use:如果您想使用更快的 PDO_ODBC,您可以使用:

//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}'; 
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';

$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);

Figured this out.想通了这一点。 Pretty simple:很简单:

 new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]",  "[username]", "[password]");

This works for me, and in this case was a remote connection: Note: The port was IMPORTANT for me这对我有用,在这种情况下是远程连接:注意:端口对我来说很重要

$dsn = "sqlsrv:Server=server.dyndns.biz,1433;Database=DBNAME";
$conn = new PDO($dsn, "root", "P4sw0rd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$sql = "SELECT * FROM Table";

foreach ($conn->query($sql) as $row) {
    print_r($row);
} 
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try {
    $conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
        array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );
} catch (PDOException $e) {
    echo ("Error connecting to SQL Server: " . $e->getMessage());
}
try
{

    $conn = new PDO("sqlsrv:Server=$server_name;Database=$db_name;ConnectionPooling=0", "", "");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch(PDOException $e)
{

    $e->getMessage();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM