SunQuest
 
       PHP
  Home arrow PHP arrow Page 2 - Executing SQL Server Stored Procedures Wit...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Executing SQL Server Stored Procedures With PHP
By: Joe O'Donnell
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 53
    2003-01-01

    Table of Contents:
  • Executing SQL Server Stored Procedures With PHP
  • Configuring PHP
  • Running the mssql_xxx functions
  • Executing stored procedures
  • Capturing output parameters and return values
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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.

    More PHP Articles
    More By Joe O'Donnell


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway