Code refactored to share common functionality between the SQLSRV and PDO_SQLSRV drivers.

SQLSRV extension is rebuilt with refactored code (no new features).

PDO::__construct(): a design change from CTP1, Connection Options now need to be specified in the DSN string instead of the driver_options[] array.

PDO::SQLSRV_ATTR_DIRECT_QUERY: new custom driver attribute to provide more flexibility to the developer.

Ability to execute two queries within the same context or different contexts (useful for using temp tables and other features that are not always available with prepared statements).

Error messages are now encoded per the setting of CharacterSet / PDO::SQLSRV_ATTR_ENCODING.

Bug fixes to both the PHP_SQLSRV and PDO_SQLSRV extensions.

Some of these changes demonstrate that the team is committed to maintaining this driver (re-factoring) and others show that they are committed to improving its quality (bug fixes). But what I want to focus on are some of the changes that will affect how you write code when using this driver.

First, let’s look at the changes in the PDO constructor (PDO::__construct()). The change here is that connection options now need to be specified in the DSN string (as opposed to the driver options array). For example, to connect to the server and specify a database, here’s what you need to do:

Note that the Server and Database are specified in the DSN connection string and are separated by a semi-colon. These options are specified as OptionName = OptionValue and are separated by semicolons. Connection options used to be specified as Key => Value pairs in the $connectionOptions array. (For a complete list of connection options, see Connection Options in the documentation.) The $connectionOptions array (which I’m now calling the $driverOptionsarray in my code examples) is now used to specify the predefined PDO attributes and the driver specific attributes PDO::SQLSRV_ATTR_ENCODING and PDO::SQLSRV_ATTR_DIRECT_QUERY (see below for information about using these). For more information, see the documentation for PDO::__construct().

Next, let’s look at the new PDO::SQLSRV_ATTR_DIRECT_QUERY attribute. The value for this attribute (true or false, the default is false) can be set on a connection in the connection options array of PDO::__construct() or by using the PDO::setAttribute() method. Setting this attribute to true allows you to preserve context from one query to the next. However, if the value is set to true, queries that are executed on the connection will not be prepared (i.e. an execution plan is not cached on the server). This can result in a slight performance hit if you want to re-execute a query several times with different parameter values. To understand this better, let’s take a look at the example that is in the driver documentation:

Because the value of SQLSRV_ATTR_DIRECT_QUERY is set to true in this example, temp tables are usable. Temp tables wouldn’t useable from query to query if the value of the attribute were false because context would not be preserved from query to query (and temp tables are context-specific)…you can test this by setting the value to false. However, if we wanted to re-execute the INSERT statement several times with different parameter values, we would lose the performance benefit of having a query plan cached on the server. So, this attribute basically gives you control over whether you want to optimize for performance of re-executed queries or a preserved context (and you now can get the benefit of parameter binding when working with temporary tables).

Lastly, let’s look at the new possibilities for error message encoding. You can now specify that error messages are encoded according the the CharacterSet connection option (if you are using the native sqlsrv extension) or according to the PDO::SQLSRV_ATTR_ENCODING attribute if you are using the PDO extension. So if you want to return errors with UTF-8 encoding, here’s how you would connect with the native driver:

This could be because that version of the driver isn't compatible with the version of PHP you are running. This topic in the documentation has a table that shows you with version of the driver you should install based on the version of PHP you are using: msdn.microsoft.com/…/cc296170(v=SQL.90).aspx. You have to pay close attention to 1) PHP 5.2 or PHP 5.3, 2) Thread-safe version of PHP or not, and 3) was PHP compiled with VC6 or VC9. If you don't know that information, I'd suggest trying other .dll files that are included in the download.