通过 PDO ODBC 将 PHP 连接到 MSSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20163776/
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 PHP to MSSQL via PDO ODBC
提问by user1477388
When I execute this code:
当我执行此代码时:
print_r(PDO::getAvailableDrivers());
It says I have the odbcdriver available.
它说我有odbc可用的驱动程序。
Array ( [0] => mysql [1] => odbc [2] => sqlite )
However, when I try to use it like so:
但是,当我尝试像这样使用它时:
$handle = new PDO("odbc:Server=dbServerIpAddress,myportnumber;Database=mydatabase", "myusername", 'mypassword');
It doesn't do anything - no errors and it doesn't work at all. It won't even execute past that line!
它什么都不做 - 没有错误,它根本不起作用。它甚至不会越过那条线执行!
How can I connect PHP to this MSSQL database via PDO and ODBC?
如何通过 PDO 和 ODBC 将 PHP 连接到这个 MSSQL 数据库?
回答by JamesP
The accepted answer is correct up to the actual PHP call. As someone has rightly commented it should be calling the odbc driver. Secondly it isn't using the Data Source Name (DSN) that has been configured in odbc.inibut is in fact creating an ad-hoc DSN. Instead:
接受的答案在实际 PHP 调用之前是正确的。正如有人正确评论的那样,它应该调用 odbc 驱动程序。其次,它不使用已在odbc.ini 中配置的数据源名称 (DSN),但实际上是在创建临时 DSN。反而:
$pdo = new PDO("odbc:mssql", "$dbuser","$dbpwd");
where mssqlrefers to the DSN object in odbc.ini
其中mssql指的是odbc.ini 中的 DSN 对象
You can create an ad-hoc DSN as follows:
您可以按如下方式创建临时 DSN:
$pd = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
where mssqlnow refers to the server object in freetds.confand FreeTDSthe driver object in odbcinst.ini
其中MSSQL现指服务器对象freetds.conf和freetds的驱动程序对象ODBCINST.INI
(this should really be a comment but I don't have the rep points).
(这真的应该是一个评论,但我没有代表点)。
回答by Benny Hill
There are several configuration files you need to have set up. /etc/odbc.ini, /etc/odbcinst.iniand /etc/freetds/freetds.conf(these locations are valid for Ubuntu 12.04 and probably correct for most *nixes).
您需要设置几个配置文件。/etc/odbc.ini,/etc/odbcinst.ini和/etc/freetds/freetds.conf(这些位置对 Ubuntu 12.04 有效并且可能适用于大多数 *nix)。
You'll need to install unixodbcand freetds(not sure what the package names are on CentOS). In Ubuntu this would be apt-get install unixodbc tdsodbc.
您需要安装unixodbc和freetds(不确定 CentOS 上的软件包名称是什么)。在 Ubuntu 中,这将是apt-get install unixodbc tdsodbc.
For help installing these, look at this question Can't Install FreeTDS via Yum Package Manager
有关安装这些的帮助,请查看这个问题Can't Install FreeTDS via Yum Package Manager
/etc/odbc.ini (this file may be empty)
/etc/odbc.ini(这个文件可能是空的)
# Define a connection to a Microsoft SQL server
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description = MSSQL Server
Driver = freetds
Database = XXXXXX
ServerName = MSSQL
TDS_Version = 7.1
/etc/odbcinst.ini
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
# Make sure you use the right driver (32-bit or 64-bit).
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
#Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf (or you may find it at /etc/freetds.conf)
/etc/freetds/freetds.conf (或者你可以在 /etc/freetds.conf 找到它)
# The basics for defining a DSN (Data Source Name)
# [data_source_name]
# host = <hostname or IP address>
# port = <port number to connect to - probably 1433>
# tds version = <TDS version to use - probably 8.0>
# Define a connection to the Microsoft SQL Server
[mssql]
host = XXXXXX
port = 1433
tds version = 7.1
You may have to change the tds version = 7.1line above depending on your version of MSSQL.
您可能需要tds version = 7.1根据您的 MSSQL 版本更改上面的行。
You will have to restart apache after you've made these changes.
进行这些更改后,您必须重新启动 apache。
In your PHP code you'll create your PDO object like this:
在您的 PHP 代码中,您将像这样创建 PDO 对象:
$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");
Note that your username may need to be in the format: domain\username.
请注意,您的用户名可能需要采用以下格式:domain\username.
Also, you will know that it worked if you execute phpinfo()in your page and search for "freetds" which will show an mssql section with freetds listed as the Library Version.
此外,如果您phpinfo()在页面中执行并搜索“freetds” ,您就会知道它是否有效,这将显示一个 mssql 部分,其中 freetds 被列为库版本。
回答by Don
If you want to directly setup a pdo odbc connection using FreeTDS driver to MS SQL server, without specifying it in configuration file freetds.conf.
如果您想使用 FreeTDS 驱动程序直接设置一个 pdo odbc 连接到 MS SQL 服务器,而无需在配置文件 freetds.conf 中指定它。
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;PORT=$portNo;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
If you have a MSSQL server that have a named instance, you can remove the port no and then modify $serverName in the format server_ip\instance_name Eg: "192.168.1.1\sqlexpress" where sqlexpress is the instance name.
如果您的 MSSQL 服务器具有命名实例,则可以删除端口号,然后以 server_ip\instance_name 格式修改 $serverName 例如:“192.168.1.1\sqlexpress”,其中 sqlexpress 是实例名称。
$connection_string = "odbc:DRIVER=FreeTDS;SERVER=$serverName;DATABASE=$dbName";
$conn = new PDO($connection_string, $dbUser, $dbPass);
Please note to configure the driver location in odbcinst.ini
请注意在odbcinst.ini中配置驱动位置
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = libtdsodbc.so
Setup = libtdsS.so

