Jump to content

Executing a SQL Server Stored Procedure in PHP


Go to solution Solved by juddadredd,

Recommended Posts

Hi all,

 

  This is just an experiment to make sure that I can get data from a Stored Proc 'IF' and when I need to, so it's basically a THOUGHT EXERCISE.

  MSSQL 2014

 

  I'm a noobie and need a hand, I've managed to get my Instance connected to the internet and I can query it using PHP and SQL, I can also look at views with no problem.

 

  I have it working as an "ADODB.Connection" and like I said it connects and I can query data and display results.

 

  Now I have coded a Stored Proc "GetMonthDays" in Sql Server:

  Which returns days 1 through xxx in a given month and also returns the Day name  eg... Sat for each date

 

  2014-01-01 Thurs

  2014-01-02 Fri

  etc...

 

  It works perfectly and very fast so All cool with that side BUT...

 

  I want to be able to query the Database through a Stored Proc, I've spent all day trying to find a way to get this to work and I've hit a wall

  Can anyone please tell me IF it's A. possible and B. maybe if they are really really nice and share with me a code example that I can change to fit my needs ?

 

  This T-SQL returns what it needs to

 

  Begin
  EXEC dbo.qselGetMonthDays '2015-01-01'
  End
 

 

  Cheers

 

  JD

Edited by juddadredd
  • Solution

Hi ya Cronix,

 

  No it's actually this just incase it will help out anyone else - it now takes input from the webpage - calls the Stored Procedure and then dumps it out with a record count

  I've also coded some other routines now that will populate Tables from a Webpage Via a Stored Procedure - Hopefully make things less hackable.

 

  Cheers All

 

<?PHP

 

// Set up strings here for DB and Server connections

 

$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn’t connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle) or die("Couldn’t open database $myDB");

echo $_GET['timestr'];
$StartDate = $_GET['timestr'];   //$StartDate = '2015-01-01'; test string

$result = mssql_query("EXEC dbo.qselGetMonthDays @StartDate = '$StartDate'", $dbhandle);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

 

while( $row = mssql_fetch_object( $result ) )
{
echo $row->DateField . " " . $row->DayField;  echo '</br>';
}

 

mssql_close($dbhandle);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.