Working with Zend Framework 2 and SQL Server

Rarely, a situation arises when we have to figure out how PHP interacts with Microsoft’s SQL Server. Whoever has worked on trying to make this work already knows it’s not a cakewalk as compared to connecting with the MySQL Server. If you are looking at this space, then i am sure you have already tried a few tricks and it still hasn’t worked.  Don’t worry too much about it, I have it all figured out and it works like a charm without any workarounds or any third party tools or packages. Just follow the steps below and you are good to go.

Prerequisites

  • SQL Server installation
  • Windows Operating System

Steps

  • Learn about your web server. See if it is a thread safe or a non-thread safe server. If you are using Zend Server to host your PHP application, then don’t try hard to understand the intricacies of the server, it is non-thread safe.
  • Once you know what type of server you are using, go ahead and download the SqlSrv extension for PHP.
  • Download and extract all the files to a folder on your desktop.
  • Download and install the Microsoft SQL Native Client if the SQL Server installation has not already installed it for you.
  • Depending on the PHP version you are using and the type of server as determined in the first step (5.3, 5.4 etc…), find the respective library from the folder on the desktop and drop it into the PHP extension folder (you can find it in the php.ini file)
  • Locate the the php.ini file in your web server and under the “Dynamic Extensions” sections add the extension info.
  • For example, if you are using Zend Server which is a non-thread safe web server and php 5.4, paste this into the php.ini file.
  • Restart the web server.

In your ZF2 application under the global.php or the local.php, configure the db adaptor as shown below:

'db'=> array(

     'driver' => 'Sqlsrv',
     'servername' => 'SERVERNAME , PORTNAME',
     'database' => DATABASENAME,
     'uid' => 'USERNAME',
     'pwd' => 'PASSWORD'
));

Make sure all the details above are configured as shown, because even a slight deviance from it will result in a connection error.

Download Links

2 thoughts on “Working with Zend Framework 2 and SQL Server”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top