Pragmatism in the real world

Zend Framework connection to SQL Server using SqlSrv

This post is part of a series about my experiences building a PHP app for Windows Server 2008 and IIS 7 for the European WinPHP Challenge 2009 which is sponsored by iBuildings, Microsoft and Leaseweb.

I haven’t managed to get much time on the challenge this week as I had hoped. Both my sons’ birthday parties have taken place along with one son’s birthday (the other’s is tomorrow).

It turns out that fellow challenger Juozas Kaziukenas also needs a connection between Zend Framework and SQL Server, so we have joined forces to create App_Db_Adapter_SqlSrv and associated classes. Juozas started a project on codeplex at and has enabled me to commit to the project too.

Juozas committed an initial implementation and so far I’ve updated the _connect() method to allow for all the parameters supported by sqlserv_connect().

My current application.ini looks like this:

resources.db.adapter = SQLSRV
resources.db.params.adapterNamespace = "App_Db_Adapter" = "RKAWIN2008SQLEXPRESS"
resources.db.params.username = "rob"
resources.db.params.password = "123456"
resources.db.params.dbname = zf-tutorial
resources.db.params.driver_options.ConnectionPooling = 1
resources.db.params.driver_options.Encrypt = 0
resources.db.params.driver_options.TransactionIsolation = SQLSRV_TXN_READ_COMMITTED

The adapter correctly converts the SQLSRV_TXN_READ_COMMITTED string from the ini file into the constant value required by the sqlsrv_connect() function using the very useful constant() function.

I had a slight hiccup with detection of the table’s primary key. Microsoft’s documentation is quite clear once you have found what you are looking for, so that was easily fixed.

At this point the adapter is working for my ZF tutorial, though more work needs to be done on it.

10 thoughts on “Zend Framework connection to SQL Server using SqlSrv

  1. So I'm trying to connect from ZF running on XAMP on OS X to sql server running on a server. Can I use sqlsrv at all on OS? Or does it only work on a windows machine? Thanks if you can help!

    1. Hi Chris,

      The Sqlsrv driver is Windows only. I have no idea how to connect to SQL Server from a non-Windows machine. Sorry.


  2. Hi Rob,

    What's about this adapter ? I don't find it on Zend Framework documentation.

  3. Hi,

    I want to connect SQLSERVER (MSSQL) with zend(MVC).

    when I am trying to connect with PDO_MSSQL getting error like
    "The mssql driver is not there"

    Please help me out.

    thanks in advance

Comments are closed.