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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13371281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:16:08  来源:igfitidea点击:

How can I connect to Sql Server from a Mac with PHP PDO?

phpsql-serverpdo

提问by Eric Z Beard

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.

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

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.

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

But I don't see it listed under PDO.

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

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

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.

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

采纳答案by Benny Hill

Does this help you?

这对你有帮助吗?

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

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

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.

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

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

这是我的 /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

[Edit by the asker]

[由提问者编辑]

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:

FreeTDS 配置是答案的前半部分。配置完成后,您应该能够从命令行运行类似的内容并进行连接:

tsql -S xyz -U username -P password

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

然后您需要使用 dblib,而不是 mssql,作为 PDO 驱动程序:

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

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

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

回答by E_p

dblibis the driver that need to be used with mssql on unix systems

dblib是 unix 系统上需要与 mssql 一起使用的驱动程序

No need for you to install anything else,

无需您安装任何其他东西,

<?php
    $dsn = 'dblib:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    $dbh = new PDO($dsn, $user, $password);

回答by Esteban Eid Jordán

After looking at many threads, I've found that the best way to connect to MSSQL from Mac OS X with PHP 7 or older is to use dblib. (Just download the correct php version)

在查看了许多线程后,我发现使用 PHP 7 或更早版本从 Mac OS X 连接到 MSSQL 的最佳方法是使用 dblib。(只需下载正确的php版本)

You can follow these instructions (ignoring the mssql.so extension) to connect very easily:

您可以按照以下说明(忽略 mssql.so 扩展名)轻松连接:

https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md

https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md

It worked perfect with OS X El Capitan, Bitnami with PHP 7.

它与 OS X El Capitan、Bitnami 和 PHP 7 完美配合。

Steps 1.- Install XCode

步骤 1.- 安装 XCode

$ xcode-select ---install

2.- Install Homebrew

2.- 安装 Homebrew

3.- Install autoconf using Homebrew.

3.- 使用 Homebrew 安装 autoconf。

$ brew install autoconf

4.- Install FreeTDS

4.- 安装 FreeTDS

$ brew install freetds

5.- Download your version of PHP Source and uncompress it.

5.- 下载您的 PHP 源版本并解压缩。

6.- Build the PDO DBLIB extension (Example for PHP 5.5.14)

6.- 构建 PDO DBLIB 扩展(PHP 5.5.14 示例)

$ cd php-5.5.14/ext/pdo_dblib
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/
$ make
$ sudo cp modules/pdo_dblib.so /usr/lib/php/extensions/no-debug-non-zts-20121212

7.- Add the .so extensio to php.ini extension=pdo_dblib.so

7.- 将 .so 扩展添加到 php.ini extension=pdo_dblib.so

8.- Restart Apache

8.- 重启 Apache

9.- Connect using the dblib dsn:

9.- 使用 dblib dsn 连接:

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

回答by holm50

Thanks Esteban for the nice guide (https://stackoverflow.com/a/37707426) which succesfully helped me install the pdo_dblib driver.

感谢 Esteban的出色指南 ( https://stackoverflow.com/a/37707426),它成功地帮助我安装了 pdo_dblib 驱动程序。

However, I was having issues connecting to my Azure SQL database from OSX 10 with PHP (5.5) and FreeTDS using the provided dblib dsn. What finally fixed it for me was appending the Azure database (m53man42a) to my username.

但是,我在使用提供的 dblib dsn 从带有 PHP (5.5) 和 FreeTDS 的 OSX 10 连接到我的 Azure SQL 数据库时遇到了问题。最终为我修复的是将 Azure 数据库 (m53man42a) 附加到我的 username

My dblib PDO connection in PHP:

我在 PHP 中的 dblib PDO 连接:

$conn = new PDO("dblib:host=azure-sql;dbname=my-database-name", 
"username@m53man42a",
"my-secret-password");

My FreeTDS.conf:

我的 FreeTDS.conf:

[azure-sql]
host = m53man42a.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520

Consider adding this as a bullet number 10 in your list... :D

考虑将其添加为列表中的第 10 号项目符号... :D

回答by xgretsch

Note that Microsoft have published a PHP7 extension for this, but if you're still on PHP5.x, that doesn't help you. I've succeeded in connecting using a different stack: freetds,odbc,pdo.

请注意,Microsoft 已为此发布了 PHP7 扩展,但如果您仍在使用 PHP5.x,那对您没有帮助。我已经成功地使用不同的堆栈进行连接:freetds、odbc、pdo。

I'm using OS X 10.11.6 (El Capitan) with Macports, PHP5.6.

我将 OS X 10.11.6 (El Capitan) 与 Macports、PHP5.6 一起使用。

I've started by creating an Azure SQL Database called mydbon a server with a name of myserver.database.windows.net. It's important to remember to open the firewall to your client IP address, which you do on the server.

我首先创建了一个 Azure SQL 数据库mydb,该数据库在名为myserver.database.windows.net. 重要的是要记住对您的客户端 IP 地址打开防火墙,这是您在服务器上所做的。

First step is to install freetds with the ODBC driver, and its PHP connector (change php56 to the correct version of your PHP):

第一步是安装带有 ODBC 驱动程序的 freetds 及其 PHP 连接器(将 php56 更改为正确的 PHP 版本):

sudo port install freetds +odbc
sudo port install php56-odbc

Next, you need to include some lines in your configuration files:

接下来,您需要在配置文件中包含一些行:

/opt/local/etc/odbcinst.ini

/opt/local/etc/odbcinst.ini

[FreeTDS]
    Description = ODBC for FreeTDS
    Driver      = /opt/local/lib/libtdsodbc.so
    Setup       = /opt/local/lib/libtdsodbc.so
    FileUsage   = 1

This tells the odbc library where to find its odbc driver.

这告诉 odbc 库在哪里可以找到它的 odbc 驱动程序。

/opt/local/etc/freetds/freetds.conf

/opt/local/etc/freetds/freetds.conf

[myserver]
    host = myserver.database.windows.net
    port = 1433
    tds version = 7.0

This tells the freetdc library where to find your server.

这会告诉 freetdc 库在哪里可以找到您的服务器。

/opt/local/etc/odbc.ini

/opt/local/etc/odbc.ini

[myds]
Description = Test for SQL Server on Azure
Driver = FreeTDS
Trace = Yes
TraceFile = /var/log/sql.log
Database = mydb
Servername = myserver
UserName = myusername
Password = mypassword
Port = 1433
Protocol = 7.0
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No

This creates a data source called mydspointing at your database, enabling you to connect with the following PHP:

这将创建一个名为myds指向您的数据库的数据源,使您能够连接以下 PHP:

$conn = new PDO('odbc:myds', 'myusername', 'mypassword');

If any of this doesn't work for you, first check that the freetds installation is correct using:

如果其中任何一个对您不起作用,请首先使用以下命令检查 freetds 安装是否正确:

tsql -S myserver -U myusername -P mypassword

And then check that the ODBC specifications are OK using:

然后使用以下命令检查 ODBC 规范是否正常:

isql -v myds myusername mypassword

Thanks to https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server, which does the equivalent job for Python and which pointed me in the right direction for all this.

感谢https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server,它为 Python 做了等效的工作,并指出我在右边这一切的方向。