laravel php7.3 连接 sqlserver
一、查看php 是否安装了 sqlsrv 扩展, 使用命令 php -m
二、如果没安装会报错 laravel could not find driver sqlsrv
三、下载安装 php 对应的sqlsrv 扩展 到目录 php 的 ext 目录下
四、更改php.ini,重启apache 或者 nginx
extension=php_sqlsrv_73_nts_x64.dll extension=php_pdo_sqlsrv_73_nts_x64.dll
五、配置env
YY_DB_CONNECTION=sqlsrv YY_DB_PORT=1433 YY_DB_HOST=192.168.101.173 YY_DB_DATABASE=U8CLOUD YY_DB_USERNAME=sa YY_DB_PASSWORD=sa123456
六、配置config目录下的 database
'connections' => [ 'sqlsrv_yongyou' => [ 'driver' => 'sqlsrv', 'host' => env('YY_DB_HOST', 'localhost'), 'port' => env('YY_DB_PORT', '1433'), 'database' => env('YY_DB_DATABASE', ''), 'username' => env('YY_DB_USERNAME', ''), 'password' => env('YY_DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => env('YY_DB_TABLEPREFIX',''), 'prefix_indexes' => true, ], ],
七、新建sqlserver 表对应的Model,选择对应的connection 和对应的表
php artisan make:model Model/YongYou/BdCubasdoc
<?php namespace App\Model\YongYou; use Illuminate\Database\Eloquent\Model; class BdCubasdoc extends Model { // protected $connection = 'sqlsrv_yongyou'; protected $table = "bd_cubasdoc"; }
八、查询测试sql server 数据
public function handle() { $rows = BdCubasdoc::where('custname', '=', '海门药业有限公司')->get()->toArray(); dump($rows); }
九、如果报以下错误、说明电脑需要安装 ODBC 驱动程序
Illuminate\Database\QueryException : SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following UR
L to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712