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 http://zfmssql.codeplex.com/ 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" resources.db.params.host = "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”
I wish I had read this post 7 days ago! I've had so many problems with MS SQL and PHP!
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!
The Sqlsrv driver is Windows only. I have no idea how to connect to SQL Server from a non-Windows machine. Sorry.
Thanks Rob! That'll save me some time from trying. :)
What's about this adapter ? I don't find it on Zend Framework documentation.
Oups, sorry I didn't see it.
Thanks a lot !
Old post but still relevant! Very happy I stumbled on this.
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
Use the SqlSrv driver.
Comments are closed.