Home>Article>PHP Framework> Laravel connection to SQL Server solution

Laravel connection to SQL Server solution

Guanhui
Guanhui forward
2020-06-18 18:04:58 5062browse

Laravel connection to SQL Server solution

1.laravel 7.0 connects to mysql by default, and the project needs to connect to sql servel (another system application)
$users = DB::connection('php_sqlsrv') ->select(…); Various errors occur.

Personal PHP native code solution:

1. Configure the php.ini file, add the dynamic library corresponding to the PHP version, and go to the official download .

extension=php_pdo_sqlsrv_73_nts.dll
extension=php_sqlsrv_73_nts.dll

2. Install the driver because I am using windows server2016, so the driver must see whether Supported.

[这个是下载地址](https://docs.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15 "这个是下载地址")

3. Restart WAMP and use the php -m command to check whether there is php_sqlsrv. The page can also be checked using phpinfo().

注意:windows环境一定要看好你的PHP版本,和path 的默认路径是不是你当前PHP版本对应上,否则你就会采坑。

Connecting to the database

$db = new \PDO("sqlsrv:Server=ip,1433;Database=sqlname", "username", "pw");

The following is all kinds of fun

$query = "Select * from table "; $res = $db->prepare($query); $res->execute(); $tmp=$res->fetchAll(\PDO::FETCH_ASSOC);

Recommended tutorial: "Laravel Tutorial"

The above is the detailed content of Laravel connection to SQL Server solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete