Home arrow PHP arrow Page 8 - Executing Microsoft SQL Server Stored Procedure from PHP on Linux
PHP

Executing Microsoft SQL Server Stored Procedure from PHP on Linux


Learn how to setup, install and configure Apache and PHP on a Linux box to execute MS SQL stored procedures. This extensive article takes you through an in-depth look on how to achieve your ideal environment.

Author Info:
By: Jack Zhang
Rating: 4 stars4 stars4 stars4 stars4 stars / 32
October 20, 2003
TABLE OF CONTENTS:
  1. · Executing Microsoft SQL Server Stored Procedure from PHP on Linux
  2. · Preparation
  3. · Installing the Apache Web Server
  4. · Installing the Apache Web Server, Cont'd
  5. · Installing FreeTDS
  6. · Modify the PHP Source Code
  7. · Modify the PHP Source Code, Cont'd
  8. · Executing MS SQL Server Procedures from PHP
  9. · Conclusion

print this article
SEARCH DEVARTICLES

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.


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