基于 Linux 的 PHP 安装连接到 MsSQL Server

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

Linux based PHP install connecting to MsSQL Server

phpsql-serverlinux

提问by David

What is the best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server.

在 Linux 机器上通过 PHP 连接到远程 Microsoft SQL Server 的最佳方法是什么。

The PHP will only ever run on a Linux box.

PHP 只能在 Linux 机器上运行。

I've been trawling for the simplest answer for a while now.

一段时间以来,我一直在寻找最简单的答案。

回答by omid

you must to install mssql driver for php on linux.
this is a best tutorialfor you.

您必须在 linux 上为 php 安装 mssql 驱动程序。
这是最适合您的教程

回答by priyolahiri

Get the sqlsrv extension from microsoft for php http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

从微软获取 php 的 sqlsrv 扩展http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

回答by Indrajeet Singh

Try: For Ubuntu

尝试:对于 Ubuntu

sudo apt-get install php5-sybase php5-odbc freetds-common

Edit freetds.conf then connect MSSQL server with this PHP.

编辑 freetds.conf 然后用这个 PHP 连接 MSSQL 服务器。

回答by Silas Palmer

Php 5.6

php 5.6

Ubuntu

Ubuntu

sudo apt-get install php5.6-sybase freetds-common libsybdb5

AWS / Centos / Redhat

AWS / Centos / 红帽

sudo yum install php56-mssql

After that, you can connect to the MsSql database through PHP with something like this:

之后,您可以通过 PHP 连接到 MsSql 数据库,如下所示:

<?php
$server = 'localhost';
$user = 'someUser';
$pass = 'somePassword';
$database = 'theDatabaseName';

try {
    $pdo = new \PDO(
        sprintf(
         "dblib:host=%s;dbname=%s",
         $server,
         $database
        ),
         $user,
         $pass
    );
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
     echo "There was a problem connecting. " . $e->getMessage();
}

$query = "SELECT * FROM TestSchema.Employees";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

var_dump($results);

You can troubleshoot MsSQL with something like this on the command line:

您可以在命令行上使用以下内容对 MsSQL 进行故障排除:

tsql -H your.server.name -p 1433 -U yourusername -P yourpassword -D yourdatabasename

To install tsql you can run this:

要安装 tsql,您可以运行以下命令:

Installation of SQL binaries for testing on any PHP version

安装 SQL 二进制文件以在任何 PHP 版本上进行测试

sudo apt install freetds-bin


Php 7+

PHP 7+

Microsoft has native drivers we can use. Full instructions are here (Redhat / Ubuntu / Others).

微软有我们可以使用的本机驱动程序。完整说明在这里 (Redhat / Ubuntu / 其他)

That link also contains required steps to install Ms SQL server on your dev machine (working on Ubuntu, doesn't work on AWS, but you can just spin up an RDS instance there). It also contains basic instructions on how to create test tables and data in the database, and PHP connectivity code.

该链接还包含在您的开发机器上安装 Ms SQL 服务器所需的步骤(在 Ubuntu 上工作,在 AWS 上不起作用,但您可以在那里启动一个 RDS 实例)。它还包含有关如何在数据库中创建测试表和数据以及 PHP 连接代码的基本说明。

You can also install the components for a newer version of PHP like this:

您还可以为较新版本的 PHP 安装组件,如下所示:

sudo apt-get install php7.2-sybase freetds-common libsybdb5

回答by Bob Walker

After searching and trying out the many suggestions here and in other posts, and spending a lot of time, a colleague suggested trying ADO.

在搜索并尝试了此处和其他帖子中的许多建议并花费了大量时间后,一位同事建议尝试使用 ADO。

As we already had an ADO enabled PHP install, it literally took less than 10 minutes to get up and running.

由于我们已经安装了支持 ADO 的 PHP,因此启动和运行只需不到 10 分钟。

If your serious about connecting from Linux PHP to MS-SQL, consider ADO.

如果您认真考虑从 Linux PHP 连接到 MS-SQL,请考虑使用 ADO。

回答by symcbean

It's interesting to note that every answer here proposes one solution to the problem rather than answering the question asked which was which is the "best" solution. Unfortunately the OP forgot to tell us what the criteria were for "best".

有趣的是,这里的每个答案都为问题提出了一个解决方案,而不是回答所提出的问题,哪个是“最佳”解决方案。不幸的是,OP 忘记告诉我们“最佳”的标准是什么。

Nobody so far has mentioned odbc. While this entails both configuring the odbc driver for connecting to the database and enabling the php extension, this method provides the best isolation of the php code from the underlying database making it easier to port the code later.

到目前为止没有人提到odbc。虽然这需要配置 odbc 驱动程序以连接到数据库并启用 php 扩展,但此方法提供了 php 代码与底层数据库的最佳隔离,从而更容易在以后移植代码。

The microsoft provided drivers for phpshould give good compatibility but only support v7 up of php.

PHP的微软提供的驱动程序应该提供良好的兼容性,但仅支持V7了PHP的。

You may prefer to stick with the functionality provided by your Linux distro to ensure you have a supportable product and/or automated patch management. An alternative to odbc here may be the freetds driverbut you didn't tell us anything about which Linux distro this is.

您可能更愿意坚持使用 Linux 发行版提供的功能,以确保您拥有可支持的产品和/或自动补丁管理。这里 odbc 的替代方案可能是freetds 驱动程序,但您没有告诉我们这是哪个 Linux 发行版。