简体   繁体   English

在Ubuntu 12.04 LTS上从CakePHP 3连接到SQL Server

[英]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. 我的设置是在Windows上工作,但我最近切换到Ubuntu 12.04 LTS,现在它将无法连接。 When I load a page where I need to talk to SQL Server, I get this error: 当我加载一个我需要与SQL Server通信的页面时,我收到此错误:

Database driver Cake\\Database\\Driver\\Sqlserver cannot be used due to a missing PHP extension or unmet dependency 由于缺少PHP扩展或未满足的依赖项,无法使用数据库驱动程序Cake \\ Database \\ Driver \\ Sqlserver

It is obvious that CakePHP can't find the SQL Server PDO driver. 很明显CakePHP找不到SQL Server PDO驱动程序。

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). 我找到了许多旧的教程来帮助我,但我最近才拿到了(我希望能够在我的CakePHP网站上使用PDO)。 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? 使用CakePHP 3.x从我的ubuntu安装连接到这个sql server数据库需要做什么?

Update (October 17th 2016): 更新(2016年10月17日):

Microsoft recently released a preview version (on their msphpsql repo) of pdo_sqlsrv and sqlsrv for Linux . 微软最近在Linux上发布了pdo_sqlsrvsqlsrv预览版 (在他们的msphpsql repo上)。 It worked successfully for me on a Ubuntu 16.04.1 LTS (Xenial Xerus) virtual machine running php7. 它在运行php7的Ubuntu 16.04.1 LTS(Xenial Xerus)虚拟机上成功运行。


Original answer: 原始答案:

As @AD7six said: 正如@ AD7six所说:

CakePHP's sql server driver is for using PDO_SQLSRV; CakePHP的sql server驱动程序用于使用PDO_SQLSRV; If you need to use ODBC, there isn't one in the core. 如果需要使用ODBC,核心中没有一个。

PDO_SQLSRV is working if you are on Windows . 如果你在Windows上, PDO_SQLSRV正在运行。 CakePHP has a driver that support it natively. CakePHP有一个本机支持它的驱动程序。

If you want to talk to a SQL Server Database from Linux , you have to use the ODBC driver . 如果要与Linux中的SQL Server数据库通信,则必须使用ODBC驱动程序 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): 之后,您将能够使用PDO(而不是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. 为odbc查找/编写CakePHP 3数据库驱动程序可能会很棒。

The new version of CakePHP supports PDO-sqlsrv which is new the MS driver that supports PHP 7 and higher. 新版本的CakePHP支持PDO-sqlsrv,这是支持PHP 7及更高版本的MS驱动程序。

http://php.net/manual/en/ref.pdo-sqlsrv.php http://php.net/manual/en/ref.pdo-sqlsrv.php

It also allows you to connect to Azure. 它还允许您连接到Azure。 Example: 例:

//Establishes the connection $conn = new PDO( "sqlsrv:server=$serverName ; Database = $database", $uid, $pwd); //建立连接$ conn = new PDO(“sqlsrv:server = $ serverName; Database = $ database”,$ uid,$ pwd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM