Executing SQL Server Stored Procedures With PHP - Configuring PHP (Page 2 of 6 )
As you may or may not know, PHP supports SQL Server with its set of mssql_xxx functions, which are similar to its mysql_xxx functions. On Windows, it's extremely easy to interface with SQL Server because of PHP's support for these functions.
If you're running Unix/Linux then unfortunately the mssql_xxx functions aren't available, and you'll need to use either ODBC or PHP's Sybase set of functions (sybase_xxx) as well as a tabular data stream protocol such as FreeTDS to work with SQL Server. In this article I'll be focusing on running PHP running on a Windows PC to connect to SQL Server running on a Windows PC, so click here if you need to learn how to setup SQL Server support for Linux/Unix.
Ok, first step is to download the support material for this article and copy php_mssql.dll to your PHP extensions directory, which you can find under the paths and directories header of your php.ini file (open it in notepad and search for "extension_dir ="). My extension directory is the same as my PHP directory, however yours might be [your php directory]\extensions.
Next, open your php.ini file (which should be c:\windows\php.ini for Win9x users and c:\winnt\php.ini for everyone else) and look for the line starting ;extension=php_mssql.dll. The semi-colon is a comment, so removing it will effectively uncomment that line. The extension=php_mssql.dll line tells PHP to load the php_mssql.dll extension library into memory, which makes the mssql_xxx set of functions available to us.
You'll also need the SQL client tools installed on the same PC as where you're running PHP. You only need one file, which is ntwdblib.dll. You should be able to find this file in the \winnt\system32 directory on your SQL Server or on your SQL Server installation CD. Copy it to \winnt\system32 on your PHP server.
Lastly, restart your web server. For IIS users, jump to a DOS window and run iisreset. For Apache users, jump to a DOS window and run apache ľk restart.
Hopefully at this point you've restarted your web server software and are ready to continue. Before we move on, create the script below and run it through your web browser, just to make sure that the extensions were registered and loaded correctly:
$myServer = "localhost";
$myUser = "sa";
$myPass = "";
$myDB = "Northwind";
$s = @mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$d = @mssql_select_db($myDB, $s)
or die("Couldn't open database $myDB");
Replace the value of the variables shown above to match the details of your SQL Server installation. If everything is working fine then when you run the script you should see nothing but a blank page. On the other hand, if you see any messages about mssql_connect and mssql_select_db not existing, then you haven't installed the extensions properly. Double check that you've put the right path to your extensions folder in the php.ini file and that you've copied the DLL into the right folder as well. On some occasions a reboot of Windows will fix the problem also.
Now that we can connect to SQL Server correctly, let's look at executing some TSQL commands as well as some stored procedures.