Connect to MSSQL from Linux Ubuntu with Symfony and Doctrine

tl;dr

  1. Install the php5-sybase package
    This will install on Ubuntu the drivers required to connect to a Microsoft SQL Server.
  2. Test connection from PHP
    We want to make sure PHP is working before moving on to Doctrine. See below for a sample test PHP script.
  3. Install the leaseweb/doctrine-pdo-dblib bundle
    Doctrine out of the box does not support connecting to a MSSQL server from Linux. This bundle “forces” Doctrine to do it.

Sample test PHP script

Here’s a short PHP script that you can run from the command line to test that the connection to MSSQL is working fine.

$dbh = new PDO ("dblib:host=192.168.0.1;dbname=mydatabase","user","password");
$stmt = $dbh->prepare("SELECT * FROM products");
$stmt->execute();
while ($row = $stmt->fetch()) {
  print_r($row);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *