Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SQL Server from CakePHP 3 on Ubuntu 12.04 LTS

My setup was working on Windows but I recently switched to Ubuntu 12.04 LTS and now it won't connect. When I load a page where I need to talk to SQL Server, I get this error:

Database driver Cake\Database\Driver\Sqlserver cannot be used due to a missing PHP extension or unmet dependency

It is obvious that CakePHP can't find the SQL Server PDO driver.

I found many old tutorials to help me but I took the most recent (I want absolutely to be able to use PDO with my CakePHP website). This is the tutorial I followed.

Using the terminal, I can access the database with this command

sqlcmd -S my.sql.server.com -U username

What do I need to do to connect to this sql server database from my ubuntu install with CakePHP 3.x?

like image 438
richerlariviere Avatar asked Oct 31 '22 22:10

richerlariviere


1 Answers

Update (October 17th 2016):

Microsoft recently released a preview version (on their msphpsql repo) of pdo_sqlsrv and sqlsrv for Linux. It worked successfully for me on a Ubuntu 16.04.1 LTS (Xenial Xerus) virtual machine running php7.


Original answer:

As @AD7six said:

CakePHP's sql server driver is for using PDO_SQLSRV; If you need to use ODBC, there isn't one in the core.

PDO_SQLSRV is working if you are on Windows. CakePHP has a driver that support it natively.

If you want to talk to a SQL Server Database from Linux, you have to use the ODBC driver. You can install the driver using this tutorial which helped me a lot. After that, you will be able to connect to your database using PDO (not CakePHP):

try {
  $query = new PDO(
    "odbc:Driver={SQL Server};Database=[Database name]; Server=[Hosame]",
    "[Username]", 
    "[Password]");
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}

Thanks everybody for your help. It could be great to find/code a CakePHP 3 database driver for odbc.

like image 130
richerlariviere Avatar answered Nov 08 '22 08:11

richerlariviere