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 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:
Install SQL binary files for testing on any version of PHP
sudo apt install freetds-bin
Php 7+
Microsoft has its own drivers that we can use. Full instructions here (Redhat / Ubuntu / Others) .
This link also contains the necessary steps to install the Ms SQL server on your developer's computer (works in Ubuntu, does not work in AWS, but you can simply deploy an RDS instance there). It also contains basic instructions on how to create test tables and data in a database, and PHP connection code.
You can also install components for a newer version of PHP as follows:
sudo apt-get install php7.2-sybase freetds-common libsybdb5
Silas palmer
source share