Linux based PHP install connecting to MsSQL Server

2020-08-25 09:31发布

问题:

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

The PHP will only ever run on a Linux box.

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

回答1:

Php 5.6

Ubuntu

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

AWS / Centos / Redhat

sudo yum install php56-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);

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

To install tsql you can run this:

Installation of SQL binaries for testing on any PHP version

sudo apt install freetds-bin

Php 7+

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

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.

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

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


回答2:

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



回答3:

Try: For Ubuntu

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

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



回答4:

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

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

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



回答5:

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".

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.

The microsoft provided drivers for php should give good compatibility but only support v7 up of 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 driver but you didn't tell us anything about which Linux distro this is.



回答6:

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