Laravel with mssql (sqlsrv) not connecting

2019-05-23 23:30发布

I was working on laravel with mysql it was working fine. But I have another scenario now. I need to connect the laravel to ms sql database which is reside on windows server. I have used the following code to connect to the DB app/config/database.php

'default' => 'sqlsrv',

'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'IP ADDRESS',
        'database' => 'DB Name',
        'username' => 'Username',
        'password' => 'password',
        'prefix'   => '',
    ),

but its showing error saying that

PDOException could not find driver

So please can any one help me to solve this ? Thank you

3条回答
祖国的老花朵
2楼-- · 2019-05-23 23:39

To extend @AmitChaudhary's answer:

For Mac OSX using MAMP

  1. Download and install pre-requisites using Homebrew (simplest option):
    FreeTDS: brew install freetds
    autconf: brew install autoconf

  2. Download PHP version source code (I used 5.6.27): http://php.net/releases/

  3. Extract source, open Terminal, and go to folder in command-line

  4. (Optional) Temporarily add your MAMP's PHP installation to the Path:
    export PATH=/Applications/MAMP/bin/php/php5.6.27/bin:$PATH

Make sure you change the version number to whatever you're using.

  1. Type which phpize to confirm that it is using your MAMP installation's PHP

  2. Configure and Make downloaded PHP source:
    ./configure --without-iconv && make

  3. Go to the Extensions folder of the downloaded PHP source:
    cd ext

  4. Go to the MSSQL Extension folder:
    cd mssql

  5. Run phpize

  6. Configure Extension using FreeTDS:
    ./configure --with-mssql=/usr/local/Cellar/freetds/1.00.23/ && make

Confirm if this is your correct FreeTDS version, it will give you an error if it can't find it.

  1. Go to the Modules folder:
    cd modules

  2. Copy the mssql.so module to your MAMP's Extension folder (for me this was) at:
    cp mssql.so /Applications/MAMP/bin/php/php5.6.27/lib/php/extensions/no-debug-non-zts-20131226/

  3. Within MAMP, look for the Edit Templates menu option (depending what version of MAMP, it should be under the MAMP menu or available in MAMP Pro via a button)

  4. Find ; Extensions and at the bottom of this section add:
    extension=mssql.so

  5. Restart your MAMP Apache server and you should be good to go!

I literally just did this but struggled to find a decent walkthrough to job my memory so hopefully this helps someone else (or probably me) in the future.

Cheers GB.

查看更多
一夜七次
3楼-- · 2019-05-23 23:45

For Linux homestead: First, ssh into your box vagrant ssh from the Homestead folder.

Command : vagrant ssh

Install the Sybase package for enabling the support for PDO and Mssql. Command : sudo apt-get install php7.0-sybase

then run php -m on ssh to make sure pdo_dblib is enable.

For Windows: you need to manually install this extension. You can download the driver from [Microsoft's website][1].

After extracting the files, please copy them to your PHP's ext directory. To make PHP load the extension, just add the following line to your PHP.ini (this is for the non-thread safe version of PHP, which you are most likely using when you have installed PHP to use IIS FastCGI, which we recommend):

extension=php_sqlsrv.dll

And restart your Apache

查看更多
太酷不给撩
4楼-- · 2019-05-24 00:01

Are you using homestead? I had that exact same problem, using MAMP. The driver was missing from my server, so I ended up switching to homestead and installing freetds there.

查看更多
登录 后发表回答