Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get SQL Server stored procedure to work?

Status
Not open for further replies.

TTEL

IS-IT--Management
Jun 20, 2004
68
GB
I am trying to access a SQL Server stored procedure from PHP and am having difficulty with the syntax.

Firstly the connection to SQL Server is established no problem but I cannot access the SP.

I have tried following two described methods I found on the web but without success.

My SP has three fields ID, NAME and EMAIL and I want to pass the ID and get back the corresponding NAME and EMAIL.

The two methods I have tried are shown below. I really need to see a worked exaple if possible.

Also how do I tell PHP which SQL Server database it is accessing?

Any help appreciated.


CAB


<?php
// connect to database
include_once('connectMSSQL.php');
$ID=1;
$NAME="";
$email="";

$qry=mssql_init("myprocedure",$connectionstring);
mssql_bind($qry, "@id", $ID, FALSE);
mssql_bind($qry, "@name",$NAME, FALSE);
mssql_bind($qry, "@email", $EMAIL,FALSE );
$result=mssql_execute($qry) or die("QUERY FAILED");
echo "<P>STORED PROC WAS SUCCESSFULLY ACCESSED";
echo $ID;
echo $NAME;
echo $EMAIL;
echo "<P>TASK COMPLETE : ";
echo date("d/m/y @ H:i:s");
?>

results in ...

Fatal error: Call to undefined function mssql_init() in c:\Inetpub\ on line 19

And ...

<?php
// connect to database
include_once('connectMSSQL.php');
$ID=1;
$NAME="";
$email="";

$qry="EXEC myprocedure @id=$ID,$NAME,$EMAIL";
$result=odbc_exec($connectionstring,$qry) or die("QUERY FAILED TO EXECUTE");
echo "<P>TEST STORED PROC ACCESS - TASK STARTED : ";
echo date("d/m/y @ H:i:s");
echo "<P>STORED PROC WAS SUCCESSFULLY ACCESSED";
echo $ID;
echo $NAME;
echo $EMAIL;
echo "<P>TASK COMPLETE : ";
echo date("d/m/y @ H:i:s");
?>

results in ...

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ','., SQL state 37000 in SQLExecDirect in c:\Inetpub\ on line 16
 
The error with the first code sample is likely because you don't have the mssql_* family of functions available in your PHP installation. Have you followed the instructions here for making the mssql_* family of functions available?


As for the second, I'm not sure, as I avoid ODBC whereever possible.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top