Connecting Tech Pros Worldwide Forums | Help | Site Map

How to call a MS SQL Server stored-procedure from PHP?

Newbie
 
Join Date: Jan 2008
Posts: 1
#1: Jan 21 '08
hi i'm rizki. can you all help me? please

i have a problem with script php to call procedure using sql server database this is the store procedure in sql server:
Expand|Select|Wrap|Line Numbers
  1. CREATE Procedure dbo.q_putaka
  2.  (
  3.   @key varchar(200),
  4.   @scope varchar(4) = null
  5.  
  6.  )
  7. As
  8.  if @scope ='1' 
  9.   execute ('select distinct author_name,book_id,remark,availability,title  from v_pustaka where  '+@key +' and DOC_TYPE IN (SELECT NM_TYPE FROM TAKA.R_TYPEDOC WHERE CONVERT(INT,KD_TYPE) <=3 AND CONVERT(INT,KD_TYPE)>=1) ')
  10.  
  11.  else
  12.   if @scope='2'
  13.    execute ('select distinct author_name,book_id,remark,availability,title  from v_pustaka where  '+@key +' and DOC_TYPE IN (SELECT NM_TYPE FROM TAKA.R_TYPEDOC WHERE CONVERT(INT,KD_TYPE) <=6 AND CONVERT(INT,KD_TYPE)>=4) ')
  14.  else
  15.  if @scope='12'
  16.   execute ('select distinct author_name,book_id,remark,availability,title  from v_pustaka where  '+@key +' and DOC_TYPE IN (SELECT NM_TYPE FROM TAKA.R_TYPEDOC) ')
  17.  
  18.  else
  19.   if @scope='123'
  20.    execute ('select distinct author_name, book_id,remark,availability,title  from v_pustaka where  '+@key +' and DOC_TYPE IN (SELECT NM_TYPE FROM TAKA.R_TYPEDOC )  union select convert(varchar,0),book_id,remark,availability,title from v_magazine where '+@key)
  21.    else
  22.       select distinct author_name, book_id,remark,availability,title  from v_pustaka 
  23.  
  24. GO
  25.  
n the problem is how to make the script php to call that store procedure?please help me n can you all tell me step by step cause i'm not familiar with script php using sql server database. thanks

nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 857
#2: Sep 1 '08

re: How to call a MS SQL Server stored-procedure from PHP?


Have a look at this:
http://dev.mysql.com/doc/refman/5.0/en/call.html

Personally I stay away from SP's as experience shows me that merging business layer and data layer causes problems further down the line. I would always keep code and data separate.

Have you thought about PHP objects?

nathj
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#3: Sep 1 '08

re: How to call a MS SQL Server stored-procedure from PHP?


Hi.

Take a look at the mssql_init and mssql_execute functions.
Newbie
 
Join Date: Sep 2008
Location: Montreal
Posts: 9
#4: Sep 5 '08

re: How to call a MS SQL Server stored-procedure from PHP?


Try. mssql_bind() Example

[PHP]<?php

$cn = mssql_connect($DBSERVER, $DBUSER, $DBPASS);
mssql_select_db($DB, $cn);

$sp = mssql_init("WDumpAdd"); // stored proc name

mssql_bind($sp, "@productname", stripslashes($newproduct), SQLVARCHAR, false, false, 150);
mssql_bind($sp, "@quantity", stripslashes($newquantity), SQLVARCHAR, false, false, 50);

mssql_execute($sp);
mssql_close($cn);

?>
[/PHP]

See Also
mssql_bind() .....
mssql_execute()
mssql_free_statement()
Newbie
 
Join Date: Oct 2008
Posts: 1
#5: Oct 2 '08

re: How to call a MS SQL Server stored-procedure from PHP?


Hi I have this SP to execute at php but I have no Idea how to do it

this is the way I execute it on MSSQL

Expand|Select|Wrap|Line Numbers
  1. DECLARE @TranID int 
  2. BEGIN TRANSACTION 
  3. EXEC sp_TransactionModify @TranID output, 1, 100.00, 0, 100.00, 123, 456, “2008-08-20”
  4. COMMIT
  5. SELECT @TranID as TransactionID
How can I do to make it work at php??

Thank you so much!!!!
Reply