[英]How to connect to SQL server from php (Laravel)
On a Linux/Ubuntu box, I'm trying to connect to an external SQL Server
database, but I only get errors.在 Linux/Ubuntu 机器上,我试图连接到外部SQL Server
数据库,但我只收到错误消息。 I know that the hostname, db name and credentials are correct, and I can't change them.我知道主机名、数据库名称和凭据是正确的,我无法更改它们。
$odbc="odbc:Driver={SQL Server};Server=$server;Database=$database;";
$db = new PDO($odbc, $user, $password);
This just gives me could not find driver
.这只是让我could not find driver
。 I have tried every tutorial I could find, and installed tons of packages, and restarted nginx over and over again.我已经尝试了我能找到的所有教程,并安装了大量软件包,并一遍又一遍地重新启动 nginx。 I don't know what to do next.我不知道接下来要做什么。
You need to install drivers for php: https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017您需要为 php 安装驱动程序: https : //docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server? view = sql-server- 2017
Then update config/database.php
to use the sqlserv connection and driver:然后更新config/database.php
以使用 sqlserv 连接和驱动程序:
'default' => env('DB_CONNECTION', 'sqlsrv'),
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'default_database'),
'username' => env('DB_USERNAME', 'default_username'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
//[...]
],
After you've done that, you can use php artisan tinker
to confirm the driver is available:完成后,您可以使用php artisan tinker
确认驱动程序可用:
>>> DB::availableDrivers()
=> [
0 => "mysql",
2 => "sqlite",
]
and test your connection:并测试您的连接:
>>> DB::connection()
=> Illuminate\Database\MySqlConnection {#161}
(Mine doesn't show sqlserv because I don't use that driver) (我的不显示 sqlserv,因为我不使用该驱动程序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.