适用于 Linux 和 PHP 5.4 的 MSSQL Server 本地 ODBC 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10728290/
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-06 06:28:00  来源:igfitidea点击:

MSSQL Server's Native ODBC Driver for Linux and PHP 5.4

phpsql-serverlinuxodbc

提问by Vinicius Garcia

I have Apache 2.2.16 and PHP 5.4.3 on a Linux Debian 6 x64.

我在 Linux Debian 6 x64 上有 Apache 2.2.16 和 PHP 5.4.3。

To install the MSSQL Server's Native ODBC Driver for Linux, I use the following instructions: http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/

要为 Linux 安装 MSSQL Server 的本机 ODBC 驱动程序,我使用以下说明:http: //www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/

I configured my odbc.ini file this way:

我以这种方式配置了我的 odbc.ini 文件:

[mydsn]
Driver      = SQL Server Native Client 11.0
Database    = datbase
Server      = xxx.xxx.xxx.xxx,port

and my odbcinst.ini this way:

和我的 odbcinst.ini 这样:

[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading=1
UsageCount=1

To test, I run the following command:

为了测试,我运行以下命令:

$ isql -v mydsn dbusername dbpassword

And I got success:

我成功了:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

Then, a use phpize to install unixODBC on PHP 5.4, using this: (The first command, ln -s ..., is used because ./configure can't find the headers of php on the default location)

然后,使用phpize在PHP 5.4上安装unixODBC,使用这个:(使用第一个命令ln -s ...,因为./configure在默认位置找不到php的头文件)

$ sudo ln -s /usr/include/php5 /usr/include/php
$ phpize
$ ./configure --with-pdo-odbc=unixODBC && make && make test
$ sudo make install

On my phpinfo() I get:

在我的 phpinfo() 上,我得到:

PDO support - enabled
PDO drivers - odbc

PDO Driver for ODBC (unixODBC) - enabled
ODBC Connection Pooling        - Enabled, strict matching

Now it's time to test everything on a PHP 5.4 script:

现在是在 PHP 5.4 脚本上测试所有内容的时候了:

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    $conn = new PDO('odbc:DSN=mydsn;UID='.$usr.';PWD='.$psw);

    $query = 'select * from my_table'; 
    $stmt = $conn->prepare($query);
    $stmt->execute();
    while ($row = $stmt->fetch()) {
        echo "<pre>";
        print_r($row);
        echo "</pre>";
    }
?>

But it doesn't work... I got this error message:

但它不起作用......我收到此错误消息:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0 
[unixODBC][Driver Manager]Can't open lib '/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0' : file not found' 
in /var/www/testemssql.php:17 
Stack trace: 
#0 /var/www/testemssql.php(17): PDO->__construct('odbc:DSN=mydsn...') 
#1 {main} thrown in /var/www/testemssql.php on line 17

So my question is: what is happen? What configuration I'm missing? How to set up correctly the MSSQL Server's Native ODBC Driver on Linux and PHP 5.4?

所以我的问题是:发生了什么?我缺少什么配置?如何在 Linux 和 PHP 5.4 上正确设置 MSSQL Server 的本机 ODBC 驱动程序?

Ps.: When I try to use the odbc_connect() PHP says the function doesn't exist.

Ps.:当我尝试使用 odbc_connect() 时,PHP 说该函数不存在。

采纳答案by Vinicius Garcia

MSSQL Server's Native ODBC Driver for Linux has a bugon it

MSSQL Server的本机ODBC驱动程序Linux有一个错误就可以了

To connect MS SQL Server correctly, use FreeTDS See more details in : PHP 5.4 on Linux: How to connect with MS SQL Server 2008?

要正确连接 MS SQL Server,请使用 FreeTDS 查看更多详细信息:Linux 上的 PHP 5.4:如何连接 MS SQL Server 2008?

回答by Ddorda

I never had the chance to try it myself, but I heard php5-sybase works fine, which I know is available for both Debian and Ubuntu in the repositories.

我从来没有机会亲自尝试,但我听说 php5-sybase 工作正常,我知道它在存储库中可用于 Debian 和 Ubuntu。

回答by Alx Russell

The same configuration, except for odbcinst.ini:

相同的配置,除了 odbcinst.ini:

[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
UsageCount=1

Retry after removing this line:

删除此行后重试:

Threading=1

And your php script works fine to me.

你的 php 脚本对我来说很好。

Hope this may help you.

希望这可以帮助你。

回答by KRavEN

If you patch and recompile php with the patch attached to https://bugs.php.net/bug.php?id=61777it will resolve the issue.

如果您使用附加到https://bugs.php.net/bug.php?id=61777的补丁修补并重新编译 php,它将解决该问题。

Also check this blog post for examples for the DSN and usage:

另请查看此博客文章以获取有关 DSN 和用法的示例:

http://strangenut.com/blogs/dacrowlah/archive/2012/04/13/installing-and-using-the-microsoft-sql-server-odbc-driver-for-linux.aspx

http://strangenut.com/blogs/dacrowlah/archive/2012/04/13/installing-and-using-the-microsoft-sql-server-odbc-driver-for-linux.aspx

回答by conclavia

I know this is a little late, but since I hit this thread while banging my head against the exact same issue here are some suggestions for anyone running into it in future :-)

我知道这有点晚了,但是由于我在遇到完全相同的问题时遇到了这个线程,因此这里有一些建议,供将来遇到它的人使用 :-)

1) Check the permissions on libsqlncli-11.0.so.1790.0 to make sure whatever user Apache is running as can access it (should have read and execute)

1) 检查 libsqlncli-11.0.so.1790.0 上的权限以确保 Apache 正在运行的任何用户都可以访问它(应该已读取并执行)

2) Use ldd to check that none of the dependencies are missing - based on the fact that isql is working above I'd say they are OK (hint: you're looking for "not found"):

2) 使用 ldd 检查是否缺少任何依赖项 - 基于 isql 在上面工作的事实,我会说它们没问题(提示:您正在寻找“未找到”):

ldd /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0

ldd /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0

3) Try running your php script from the command line rather than through Apache. If it works like that, go to stop 4. If it doesn't, I'd suggest running through strace to see what it is actually doing.

3) 尝试从命令行而不是通过 Apache 运行您的 php 脚本。如果它像那样工作,请转到第 4 步。如果没有,我建议运行 strace 以查看它实际在做什么。

4) This is the one that did it for me! Try turning off SELinux (i.e. set to not enforcing / permissive mode) and hitting the page in Apache again. I'm not sure exactly what it was blocking (haven't had time or inclination to get into the details yet) but as soon as it was off everything worked like a charm. For anyone with the inclination I'm you could dig into it and figure out how to fix this without disabling completely :-)

4)这是为我做的!尝试关闭 SELinux(即设置为不强制/许可模式)并再次点击 Apache 中的页面。我不确定它到底阻碍了什么(还没有时间或不想深入了解细节)但是一旦它关闭,一切都像魅力一样。对于任何有这种倾向的人,我是你可以深入研究并弄清楚如何在不完全禁用的情况下解决这个问题:-)

Exact commands for disabling SELinux may vary based on your OS but for me (on CentOS) this worked:

禁用 SELinux 的确切命令可能因您的操作系统而异,但对我来说(在 CentOS 上)这有效:

http://rbgeek.wordpress.com/2012/08/06/how-to-disable-selinux-on-centos-without-rebooting/

http://rbgeek.wordpress.com/2012/08/06/how-to-disable-selinux-on-centos-without-rebooting/

Good luck!

祝你好运!