There seems to be very little in the way of examples that show how to use the Zend framework to connect to a Microsoft SQL Server database, so here is what worked for me:
First up, the Zend Framework Programmer’s Reference Guide is the place to start, it talks about the Zend_Db_Adapter object that can be used to connect to various database types. Of course the example is for MySQL with little little mention of SQL Server except that ‘Pdo_Mssql’ should be specified as the adapter type and that the PHP extensions ‘pdo’ and ‘pdo_mssql’ are required.
Step 1 : Compiling PHP with PDO/PDO MSSQL support
You may need to recompile PHP with support for the forementioned extensions and install FreeTDS (needed to connect to SQL Server):
./configure … –with-pdo –with-pdo-dblib=/usr/local/freetds –with-mssql=/usr/local/freetds –with-zlib …
note: The location of your FreeTDS installation may differ
Once you have successfully compiled PHP (configure, make, make install etc.) then you should be able to use the Zend Framework to connect to SQL Server.
Step 2 : Install the Zend Framework
If you have not already done so, download and install the Zend Framework according to the install.txt file. Just decompress the archive and ensure the framework ‘library’ folder is part of the default PHP include directory, one place to set this is in the php.ini file. Not sure of where your php.ini file is? Call the PHP phpinfo(); command.
Step 3 : Setup you SQL Server database in FreeTDS
Your FreeTDS configuration file freetds.conf stores the database connections and is usually located at /usr/local/freetds/etc/freetds.conf. Check the FreeTDS documentation for instructions on how to define your connection.
Step 4 : Using the Zend_Db_Adapter object to connect to SQL Server
Use the following code:
require_once ‘Zend/Db.php’;
$params = array(
‘host’=>’HOSTNAME’,
‘username’=>’USERNAME’,
‘password’=>’PASSWORD’,
‘dbname’=>’YOUR_FREETDS_DB_NAME’,
‘pdoType’=>’dblib’ //You may want to try ‘mssql’ or ‘freetds’ here?
);
$db = Zend_Db::factory(’Pdo_Mssql’, $params);
$test = $db->fetchOne(”SELECT somefield FROM sometable”);
print $test;