php 使用 SQL Server 驱动程序通过 PDO 连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12747554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to SQL Server through PDO using SQL Server Driver
提问by Drew
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!
是否有任何很好的例子来说明如何正确地做到这一点?如果我应该通过其他方法执行此操作,请告诉我。谢谢!
回答by MatthewMcGovern
Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:
嗯,关于 PDO 的最好的部分是它很容易访问任何数据库。如果您已经安装了这些驱动程序,您应该能够执行以下操作:
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
回答by Jan
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);
回答by Drew
Figured this out. Pretty simple:
想通了这一点。很简单:
new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]", "[username]", "[password]");
回答by Luis
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);
}
回答by hassan javaid
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();
}
回答by mirzaei.sajad
$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());
}

