I am having a problem getting a result set from my Sql Server 2008 stored procedure when calling it using PHP. When I execute the stored procedure from within sql, I get the results, so I know the issue isnt with the stored procedure(below).
Sql Stored Procedure
USE [HRAPPS]
GO
/****** Object: StoredProcedure [dbo].[sp_GetSecurityAnswer] Script Date: 10/21/2011 08:38:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetSecurityAnswer]
(
@ThisContactId INT, @ThisQuestionId INT, @ThisAnswer varchar(255)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
a.Cont_id,
a.QuestionId
FROM dbo.sec_answers a
WHERE a.cont_id = @ThisContactId and
a.QuestionId = @ThisQuestionId and
lower(a.Response) = lower(@ThisAnswer)
END
Here is the PHP code that calls the stored procedure. Note: The connection to the database works, so I know that it isnt the issue.
*PHP Code *
if(!$dbCon=mssql_connect(DBHOST,DBUSER,DBPASS))
{
$error = "Unable to Connect to Database. Please Try Again Later.";
}
else
{
$stmt = mssql_init("sp_GetSecurityAnswer", $dbCon);
mssql_bind($stmt, "@ThisContactId", $_SESSION['thisContId'], SQLINT4, false, false, 6);
mssql_bind($stmt, "@ThisQuestionId", $_SESSION['SecQuestion'], SQLINT4, false, false, 2);
mssql_bind($stmt, "@ThisAnswer", $_SESSION['ThisAnswer'], SQLVARCHAR, false, false, 255);
$Security_Verification_Result = mssql_execute($stmt, false);
IF(!mssql_num_rows($Security_Verification_Result))
{
ECHO "You have Incorrectly answered your selected Security Verification Question, Please go back and try again";
EXIT();
}
ELSE
{
header("Location: some_url?userfullname=".$_SESSION['userfullname'] .'&cont_id='.$_SESSION['ThisContId']);
}
}
exit();
End of PHP Code
So what am I missing guys(and gals too!)?? any help would be greatly appreciated 😀
thanks in advance
your code:
what is up with the optional max length parameter? shouldn’t they both be -1 (max len) or just don’t pass them in?? This optional parameter is only valid with strings, see mssql_bind
if this doesn’t work, your only other options are to echo the parameters in PHP and record them to a log table from within the procedure. This way you can be sure you are sending and receiving the proper values in the procedure.
EDIT
creating and inserting into a log from SQL is easy, here’s how:
create a table:
Add this in the procedure right after the
SET NOCOUNT ON;After you run your application use this to see what you get in the log:
that should give you a better idea of what the actual parameters SQL Server is using with in the query.