如果你在谷歌上搜索这个问题,你会发现很多不正确、误导和过时的信息。令人惊讶的是,Stack Overflow 上并没有一个可靠的答案,所以我们应该改变它。
我正在使用 Apache 和 PHP 的 Mac 端口安装。我已经安装了 php5-mssql,在我的 phpinfo() 页面上可以看到 mssql。
但我没有看到它列在 PDO 下。
PDO support enabled
PDO drivers dblib, mysql, odbc, pgsql
mssql 是否与 PDO 无关?是否有另一个驱动程序可以在 Mac 上使用 PDO 连接到 SqlServer 数据库?看来这应该是可能的。
最佳答案
这对你有帮助吗?
http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/
我使用 FreeTDS 从 Linux 服务器连接到 Microsoft SQL 服务器,看起来上面链接中的人已经使用 FreeTDS 从 Mac 连接。
这是我的/etc/freetds/freetds.conf 文件(我添加的唯一部分是在 XYZ 服务器的最后):
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[xyz]
host = xyz
port = 1433
tds version = 8.0
[提问者编辑]
FreeTDS 配置是答案的前半部分。配置完成后,您应该能够从命令行运行类似的东西并连接:
tsql -S xyz -U username -P password
然后你需要使用 dblib,而不是 mssql,作为 PDO 驱动程序:
$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname",
"$dbuser","$dbpwd");
其中 $dbhost 是来自 freetds.conf 文件的名称
关于php - 如何使用 PHP PDO 从 Mac 连接到 Sql Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13371281/