9

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
  • Check out this tutorial: aka.ms/sqldev Commented Dec 14, 2016 at 16:25

6 Answers 6

18

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
Sign up to request clarification or add additional context in comments.

1 Comment

The above did not work for me in docker with PHP 5.6 but this did the trick -> github.com/docksal/docksal/issues/473#issuecomment-374855050
5

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

Comments

5

Try: For Ubuntu

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

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

Comments

0

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.

2 Comments

Suggestions should be posted as comments. Only answer should by a post
My apologies if I don't understand the protocal here, but I'm pretty sure I gave pretty good answer. The question was: 'What is the best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server.' My answer is: The best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server is to use the PHP ADO library. There are many examples. Here's the first one I found: stackoverflow.com/questions/10881599/php-adodb-mssql-connection
0

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.

Comments

-1

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

3 Comments

Nope. you can download and compile source as well.
Not sure you will be able to compile it to work on a linux platform since it requires SQL Server Native Client which is provided for Windows only
Microsoft released a Linux Driver for PHP. Check it out here: github.com/Azure/msphpsql/tree/PHP-7.0-Linux. Getting instructions here: github.com/Azure/msphpsql/blob/PHP-7.0-Linux/LinuxTutorial.md

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.