PHP
  Home arrow PHP arrow Page 8 - Executing Microsoft SQL Server Stored Proc...
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 Microsoft SQL Server Stored Procedure from PHP on Linux
By: Jack Zhang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 31
    2003-10-20

    Table of Contents:
  • Executing Microsoft SQL Server Stored Procedure from PHP on Linux
  • Preparation
  • Installing the Apache Web Server
  • Installing the Apache Web Server, Cont'd
  • Installing FreeTDS
  • Modify the PHP Source Code
  • Modify the PHP Source Code, Cont'd
  • Executing MS SQL Server Procedures from PHP
  • 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
     
     
    ADVERTISEMENT


    Executing Microsoft SQL Server Stored Procedure from PHP on Linux - Executing MS SQL Server Procedures from PHP


    (Page 8 of 9 )

    On my Windows box (home2k), I use Microsoft SQL Server query analyzer to create the following stored procedure on pubs database (I will use this stored procedure on Sybase SQL Server with pubs2 database in my next article):

    CREATE PROC sp_GetBooksByPrice
    @minPrice money,
    @maxPrice money,
    @lowestPricedBook varchar(100) OUTPUT,
    @highestPricedBook varchar(100) OUTPUT
    AS
    DECLARE @realminPrice money,  @realmaxPrice money, @totalBooks int
    SELECT @realminPrice = min(price) FROM titles WHERE price >=@minPrice
    SELECT @realmaxPrice = max(price) FROM titles WHERE price <
    =@maxPrice
    SELECT @lowestPricedBook =title FROM titles WHERE price = @realminPrice
    SELECT @highestPricedBook =title  FROM titles WHERE price = @realmaxPrice
    SELECT @totalBooks = COUNT(title)  FROM titles WHERE price >= @minPrice AND price <= @maxPrice
    RETURN  @totalBooks
    GO

    On the Red Hat side, use an editor to create the following file called sp_test.php:

    <?php
    $myServer = "home2k";
    $myUser = "sa";
    $myPass = "";
    $myDB = "pubs";

    $s = mssql_connect($myServer, $myUser, $myPass)
    or die("Couldn't connect to SQL Server on $myServer");

    mssql_select_db($myDB, $s)
    or die("Couldn't open database $myDB");

    $proc = mssql_init("sp_GetBooksByPrice", $s);

    $minPrice = 2.00;
    $maxPrice = 20.00;
    $lowestPricedBook = "";
    $highestPricedBook = "";
    $numBooks = 0;

    // Bind the parameters

    mssql_bind($proc, "@minPrice", $minPrice, SQLFLT8);
    mssql_bind($proc, "@maxPrice", $maxPrice, SQLFLT8);
    mssql_bind($proc, "@lowestPricedBook", $lowestPricedBook, SQLVARCHAR, TRUE, FALSE,100);
    mssql_bind($proc, "@highestPricedBook", $highestPricedBook, SQLVARCHAR, TRUE, FALSE,100);

    // Bind the return value

    mssql_bind($proc, "RETVAL", $numBooks, SQLINT2);

    mssql_execute($proc);
    mssql_free_statement ($proc);
    mssql_close($s);

    echo "<h2>There were $numBooks Books returned.</h2>";
    echo "The lowest price book was: <b>$lowestPricedBook</b>.<br>";
    echo "The highest price book was: <b>$highestPricedBook</b>.";
    ?>

    Save the file in /usr/local/Apache2/htdocs, open your browser, and input http://localhost/sp_test.php in address bar. It is useful when you debug your PHP code with MS SQL Server.

    More PHP Articles
    More By Jack Zhang


     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek