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

How can I connect to Sql Server from a Mac with PHP PDO?(如何使用 PHP PDO 从 Mac 连接到 Sql Server?)
本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)