Home > PHP Framework > Laravel > body text

Laravel connection to SQL Server solution

Guanhui
Release: 2020-06-18 18:04:58
forward
5149 people have browsed it

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 "这个是下载地址")
Copy after login

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版本对应上,否则你就会采坑。
Copy after login

Connecting to the database

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

The following is all kinds of fun

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

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template