通过 PDO ODBC 将 PHP 连接到 MSSQL [英] Connect PHP to MSSQL via PDO ODBC

查看:37
本文介绍了通过 PDO ODBC 将 PHP 连接到 MSSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行这段代码时:

print_r(PDO::getAvailableDrivers()); 

它说我有 odbc 驱动程序可用.

It says I have the odbc driver available.

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!

如何通过 PDO 和 ODBC 将 PHP 连接到这个 MSSQL 数据库?

How can I connect PHP to this MSSQL database via PDO and ODBC?

推荐答案

您需要设置几个配置文件./etc/odbc.ini/etc/odbcinst.ini/etc/freetds/freetds.conf(这些位置对 Ubuntu 有效12.04 并且可能适用于大多数 *nixes).

There are several configuration files you need to have set up. /etc/odbc.ini, /etc/odbcinst.ini and /etc/freetds/freetds.conf (these locations are valid for Ubuntu 12.04 and probably correct for most *nixes).

您需要安装 unixodbcfreetds(不确定 CentOS 上的软件包名称是什么).在 Ubuntu 中,这将是 apt-get install unixodbc tdsodbc.

You'll need to install unixodbc and freetds (not sure what the package names are on CentOS). In Ubuntu this would be apt-get install unixodbc tdsodbc.

有关安装这些的帮助,请查看此问题 Can't Install FreeTDS via百胜包管理器

For help installing these, look at this question Can't Install FreeTDS via Yum Package Manager

/etc/odbc.ini (这个文件可能是空的)

/etc/odbc.ini (this file may be empty)

# 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(或者您可以在/etc/freetds.conf 中找到)

/etc/freetds/freetds.conf (or you may find it at /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

您可能需要根据您的 MSSQL 版本更改上面的 tds version = 7.1 行.

You may have to change the tds version = 7.1 line above depending on your version of MSSQL.

进行这些更改后,您必须重新启动 apache.

在您的 PHP 代码中,您将像这样创建 PDO 对象:

In your PHP code you'll create your PDO object like this:

$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");

请注意,您的用户名可能需要采用以下格式:domainusername.

Note that your username may need to be in the format: domainusername.

此外,如果您在页面中执行 phpinfo() 并搜索freetds",它将显示一个 mssql 部分,其中 freetds 被列为库版本,您就会知道它有效.

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.

这篇关于通过 PDO ODBC 将 PHP 连接到 MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆