Friday, March 12, 2010

trying the sql server driver for php on windows 2008 - part 2

Overview of the complete function list.
Details to follow, but just from the function list you may see, that a simple search-and-replace will do the work to switch from mssql to sqlsrv-functions in a very simple case only.


old php driver (mssql_)
sqlsrv php driver Description
Server and Connection

mssql_connect sqlsrv_connect Open MS SQL server connection / Creates and opens a connection.
mssql_pconnect - Open persistent MS SQL connection
mssql_close sqlsrv_close Close MS SQL Server connection / Closes a connection. Frees all resources associated with the connection.
mssql_select_db - Select MS SQL database
- sqlsrv_configure Changes error handling and logging configurations.
- sqlsrv_server_info Provides information about the server.
- sqlsrv_client_info Provides information about the client.
- sqlsrv_get_config Returns the value of the specified configuration setting.
mssql_min_error_severity - Sets the minimum error severity
mssql_min_message_severity - Sets the minimum message severity



querying data

mssql_query sqlsrv_query Send MS SQL query / Prepares and executes a Transact-SQL query.
mssql_free_statement sqlsrv_free_stmt Free statement memory / Closes a statement. Frees all resources associated with the statement.



mssql_bind - Adds a parameter to a stored procedure or a remote stored procedure
mssql_data_seek - Moves internal row pointer
mssql_execute sqlsrv_execute Executes a stored procedure on a MS SQL server database / Executes a prepared statement.
mssql_rows_affected sqlsrv_rows_affected Returns the number of records affected by the query / Returns the number of modified rows.
mssql_result - Get result data
- sqlsrv_fetch Makes the next row of data available for reading.
mssql_free_result - Free result memory




sqlsrv_prepare Prepares a Transact-SQL query without executing it. Implicitly binds parameters.
mssql_next_result sqlsrv_next_result Move the internal result pointer to the next result / Makes the next result available for processing.
mssql_init - Initializes a stored procedure or a remote stored procedure
- sqlsrv_begin_transaction Begins a transaction.
- sqlsrv_commit Commits a transaction.
- sqlsrv_cancel Cancels a statement; discards any pending results for the statement.
- sqlsrv_rollback Rolls back a transaction.



getting results

mssql_fetch_array sqlsrv_fetch_array Fetch a result row as an associative array, a numeric array, or both / Retrieves the next row of data as a numerically indexed array, an associative array, or both.
mssql_fetch_assoc - Returns an associative array of the current row in the result
mssql_fetch_batch - Returns the next batch of records
mssql_fetch_field sqlsrv_get_field Get field information / Retrieves a field in the current row by index. The PHP return type can be specified.
mssql_fetch_object sqlsrv_fetch_object Fetch row as object / Retrieves the next row of data as an object.
mssql_fetch_row
Get row as enumerated array
- sqlsrv_fetch
Metadata

mssql_field_length - Get the length of a field
mssql_field_name - Get the name of a field
mssql_field_seek - Seeks to the specified field offset
mssql_field_type - Gets the type of a field
mssql_num_fields sqlsrv_num_fields Gets the number of fields in result / Retrieves the number of fields in an active result set.
mssql_num_rows sqlsrv_num_rows Gets the number of rows in result / Reports the number of rows in a result set.

sqlsrv_field_metadata Returns field metadata.

sqlsrv_has_rows Detects if a result set has one or more rows.
Error-handling

mssql_get_last_message - Returns the last message from the server
- sqlsrv_errors Returns error and/or warning information about the last operation.



Other

mssql_guid_string - Converts a 16 byte binary GUID to a string
- sqlsrv_send_stream_data Sends up to eight kilobytes (8 KB) of data to the server with each call to the function.

Tuesday, February 16, 2010

trying the sql server driver for php on windows 2008 - part 1

We are talking about a Windows 2008 Server 64 bit installation here and trying to set up apache (with ssl), php and a php application that uses a connection to a MS SQL Server (2005 or higher).

Another prerequisite: we want to use the most recent available versions for all involved products.
(it's February 2010 so we are using Apache 2.2.14, php 5.3.1, w2008SR2).

I will try to point out some of the points that you may stumble upon (used apache/php/sqlsrv file-versions, comparison of mssql_* vs. sqlsrv_* driver functions, specific issues with the sqlsrv_* functions) in several blog posts over the next few weeks.

So let's go.
You may have noticed that the php_mssql.dll driver on windows does no longer work with php 5.3 or higher and even with php 5.2.11 (upon apache start you get these typical "file not found" errors that just point out that the module can not be loaded for some reason.
It has not been forgotten and it will not come back in a next version afaik.

Basically you have 3 options:
  1. You can give up here and switch back to php 5.2.10 or an older version. And I definitely advise you to do so if you don't want to get in lots of troubles...
  2. switch the webserver part of your application to linux: if you use the freetds driver (that just replaces the original mssql_* functions 1:1) you are done and you will have advanced sql server functionality in this driver (utf8 support etc.).
  3. rewrite your application to use the new and opensource sql-server-driver for php that was written by Microsoft (you are reading right here, MS is writing an opensource driver for php)! The thing is: They dont' just take over the mssql_* functions like freetds does. The functions all start with 'sqlsrv_'. And they have (in some cases) completely different semantics. It's not just a search and replace work, it's a lot to do!
  4. We are not talking about php database abstraction layers for php (DBA, ODBC, PDO, whatever) and functions here. But this may also be an option for you.
We will focus on option 3.

First issue: Installing the Server environment

So you download the latest apache from an apache mirror, download php, the sql server driver and start?
Uhm... not really. Let's see:
  1. We will start by downloading php binaries.
    But wait: For php 5.3 there are 12 different possible files to download. And they are all x86? So wich is the right one?
    If you prefer the installer, the zip or the debug-zip is a personal-flavour thing (unless you are developing php itself I don't recommend the debug-zips). I use a zip-File, because I want as much control as possible.
    We will use a VC9 file. That means: it has been compiled using visual studio 2008 (the most recent Visual Studio version). This is very important to keep in mind for later!
  2. Now we need to find an apache binary that will work with the downloaded php version.
    I recommend to try apachelounge, (read the first sentence on the donwload page wich states, that the donwloadable binary is compiled with VC9 also, but the original apache binaries from apache.org are still compiled with VC6!)
  3. Now we go to the MS Download page to get the SQL Server driver for PHP binary. The current version is 1.1.
You think you can start now? Uhm... not really. We have to download some additional components to make everything work:
  • The SQL Server driver for php is actually just a wrapper for the SQL Server Native Client database connector driver. So we need the Native Client driver as well. There is a standalone version available, but if you are running the DB (SQL Server 2005 or higher) on the same host you already have this component installed. For all others: Download it from the SQL Server 2008 Feature pack download page (scroll down to "Microsoft SQL Server 2008 Native Client"). You can use the x86 or the 64 bit version. I will use the 64bit sqlncli.msi (be careful if you download both: there is no difference in the file name!)
  • Now as we are using software that has been compiled using Visual Studio, we need the right version of the VC runtime environment: Microsoft Visual C++ 2008 Redistributable Package (x86)
  • and (if you have downloaded the 64-bit Native client) the Microsoft Visual C++ 2008 Redistributable Package (x64) as well (!).
    Be sure to not mix up the different versions (there are also 2005- Versions and 2008 SP1 versions available - but these do not work in our environment)! Again: the download filenames are the same (
    vcredist_x64.exe / vcredist.exe for all versions), so be careful!
Remember to check the certificate or md5 sum of the downloaded files (to check the file hashes I use the windows hashtab explorer extension - the coolest thing ever ;-)
And install the components.
Now you can install the apache, include php and load the (right) sql server driver file via php.ini. According to what we have downloaded we will use the VC9 thread safe php 5.3 file, that is extension=php_sqlsrv_53_ts_vc9.dll
Maybe use the absolute path to where you unzipped the files.

If you now run php info, you will get the
sqlsrv support enabled
block somwhere at the end.

That's it for today. Next time we are comparing the availabe functions (sqlsrv vs mssql).

One short note: I will not be able to give support (unless you pay me for). However if you post a comment, other people may know the answer and post it as comment as well.