March 17, 2018

XAMPP - PHP - Connect with MS SQL Server 2008 R2 in Laravel

Few days back I got stuck in this situation, I was developing PHP web application with Laravel 5.6 framework, the problems comes when I tried to connect with MS SQL Server rather than MySQL or MariaDB (that comes as default in XAMPP package), It started giving me error like driver not found. Here is how I solved this problem.

First you need to download Microsoft Drivers 4.3 for PHP for SQL Server, then extract the files to some folder. Copy the file php_pdo_sqlsrv_71_ts_x86.dll to php/ext folder (from XAMPP installation's root folder). Now we have to instruct PHP to use this extension.

Open php/php.ini file and move the cursor to extensions section. Add following new line of extension which directs PHP to use SQL Server driver's dll.

extension=php_pdo_sqlsrv_71_ts_x86.dll

We have successfully set-up our PHP environment with MS SQL Server Driver.

Don't forget to restart your server afterwards.

Now you should be able to successfully connect with SQL Server from PHP.

Here comes the Laravel part. You have to specify connection string parameters in .env file of Laravel root folder.

DB_HOST=MY-PC\MYSQL2008R2
DB_PORT=1433
DB_DATABASE=ITEMMASTER
DB_USERNAME=myuser
DB_PASSWORD=mypassword

Or alternative way is to define connection parameters in config/database.php file.

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => 'MY-PC\MYSQL2008R2',
            'port' => '1433',
            'database' => 'ITEMMASTER',
            'username' => 'myuser',
            'password' => 'mypassword',
            'charset' => 'utf8',
            'prefix' => '',
        ],

Now Laravel website should be able to connect with SQL Server database.

I hope you found this post helpful. Do you agree? Share your thoughts in the comments below!

3 comments:

  1. I was barely amazed at how you had written this content. Please keep posting.
    PHP Institutes in Chennai
    PHP Training Center in Chennai

    ReplyDelete
  2. Wonderful Blog on recent updates, I have updated my knowledge through your blog, Thanks admin for sharing the recent information.
    website design classes
    web designing classes in chennai

    ReplyDelete
  3. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.

    AngularJS5 Online Course

    ReplyDelete