Home arrow PHP arrow Page 2 - Executing SQL Server Stored Procedures With PHP
PHP

Executing SQL Server Stored Procedures With PHP


In this article Joe looks at how to connect to an SQL Server 2000 database using PHP's set of mssql_xxx functions, and also how to execute commands and stored procedures against that database.

Author Info:
By: Joe O'Donnell
Rating: 5 stars5 stars5 stars5 stars5 stars / 93
January 01, 2003
TABLE OF CONTENTS:
  1. · Executing SQL Server Stored Procedures With PHP
  2. · Configuring PHP
  3. · Running the mssql_xxx functions
  4. · Executing stored procedures
  5. · Capturing output parameters and return values
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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:

<?php

$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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials