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

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

问题描述

执行此代码时:

print_r(PDO::getAvailableDrivers()); 

它说我有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!

如何通过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.

有关安装这些工具的帮助,请查看以下问题无法通过以下方式安装FreeTDS:百胜餐饮套餐经理

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");

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

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

此外,如果您在页面中执行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天全站免登陆