如何使用 PHP PDO 从 Mac 连接到 Sql Server?

时间:2023-05-07
本文介绍了如何使用 PHP PDO 从 Mac 连接到 Sql Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如果你在谷歌上搜索这个问题,你会发现很多不正确、误导性和过时的信息.令人惊讶的是,Stack Overflow 上没有可靠的答案,所以我们应该改变它.

If you search Google for this question, you will find a lot of incorrect, misleading, and outdated information. Surprisingly, there isn't a solid answer on Stack Overflow, so we should change that.

我使用的是 Mac 端口安装的 Apache 和 PHP.我已经安装了 php5-mssql,我可以在我的 phpinfo() 页面上看到 mssql.

I am using the Mac port installation of Apache and PHP. I have installed php5-mssql, and I can see mssql on my phpinfo() page.

但我没有看到它列在 PDO 下.

But I don't see it listed under PDO.

PDO support enabled
PDO drivers     dblib, mysql, odbc, pgsql 

mssql 与 PDO 无关吗?是否有其他驱动程序可以在 Mac 上使用 PDO 连接到 SqlServer 数据库?看起来这应该是可能的.

Is mssql not associated with PDO? Is there another driver that can be used on a Mac to connect to a SqlServer database using PDO? Seems like this is something that should be possible.

推荐答案

这对您有帮助吗?

http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/

我使用 FreeTDS 从 Linux 服务器连接到 Microsoft SQL 服务器,看起来上面链接中的人使用 FreeTDS 从 Mac 连接.

I use FreeTDS to connect to Microsoft SQL servers from a Linux server and it looks like the person in the link above has used FreeTDS to connect from a Mac.

这是我的/etc/freetds/freetds.conf 文件(我添加的唯一部分是在 XYZ 服务器的最后):

Here is my /etc/freetds/freetds.conf file (the only part I added was at the very end for the XYZ server):

[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 配置是答案的前半部分.配置完成后,您应该能够从命令行运行类似的操作并进行连接:

FreeTDS configuration is the first half of the answer. Once it's configured you should be able to run something like this from the command line and connect:

tsql -S xyz -U username -P password

那么你需要使用dblib,而不是mssql作为PDO驱动:

Then you need to use dblib, not mssql, as the PDO driver:

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

其中 $dbhost 是 freetds.conf 文件中的名称

Where $dbhost is the name from the freetds.conf file

这篇关于如何使用 PHP PDO 从 Mac 连接到 Sql Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PDO 和 MariaDB 下一篇:在 PHP 中创建到 PDO 的连接时出错

相关文章