基于Linux的PHP安装连接到MsSQL Server [英] Linux based PHP install connecting to MsSQL Server

查看:204
本文介绍了基于Linux的PHP安装连接到MsSQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

PHP只能在Linux机器上运行.

The PHP will only ever run on a Linux box.

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

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

推荐答案

Php 5.6

Ubuntu

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

AWS/Centos/Redhat

sudo yum install php56-mssql

之后,您可以使用以下类似的方法通过PHP连接到MsSql数据库:

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

<?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);

您可以在命令行中使用类似以下的方法对MsSQL进行故障排除:

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

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

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

To install tsql you can run this:

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

Installation of SQL binaries for testing on any PHP version

sudo apt install freetds-bin


Php 7 +

Microsoft有我们可以使用的本机驱动程序.完整说明位于此处(Redhat/Ubuntu/其他).


Php 7+

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

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

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.

您还可以像这样安装更新版本的PHP的组件:

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

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

这篇关于基于Linux的PHP安装连接到MsSQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆