Sql存储过程/ Php问题

时间:2011-10-21 12:52:26

标签: php sql sql-server

使用PHP调用时,我在使用Sql Server 2008存储过程获取结果集时遇到问题。当我从sql中执行存储过程时,我得到结果,所以我知道问题不在于存储过程(如下)。 Sql存储过程

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

以下是调用存储过程的PHP代码。注意:与数据库的连接有效,所以我知道这不是问题。

* PHP代码*

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();

PHP代码结束

那么我想念的是什么(和gals一样!)?任何帮助将不胜感激:D 提前谢谢

1 个答案:

答案 0 :(得分:0)

您的代码:

mssql_bind($stmt, "@ThisContactId", $_SESSION['thisContId'], SQLINT4, false, false, 6);
mssql_bind($stmt, "@ThisQuestionId", $_SESSION['SecQuestion'], SQLINT4, false, false, 2);
//        scroll right-->>                                                           ^^

可选的最大长度参数是什么?它们不应该是-1(最大len)或者只是不传递它们?此可选参数仅对字符串有效,请参阅mssql_bind

如果这不起作用,您唯一的其他选项是回显PHP中的参数并将其记录到过程中的日志表中。这样,您可以确保在过程中发送和接收正确的值。

修改
从SQL创建和插入日志很简单,方法如下:

创建一个表:

CREATE TABLE MyLog (LogID int not null identity(1,1) primary key
                   ,LogDate datetime not null default GETDATE()
                   ,LogValue varchar(max) not null
                   )

SET NOCOUNT ON;

之后的程序中添加此内容
INSERT INTO MyLog 
    (LogValue) 
    VALUES (ISNULL(OBJECT_NAME(@@PROCID), 'unknown')
               +', '+ISNULL('"'+CONVERT(varchar(15),@ThisContactId)+'"','NULL')
               +', '+ISNULL('"'+CONVERT(varchar(15),@ThisQuestionId)+'"','NULL')
               +', '+ISNULL('"'+@ThisAnswer+'"','NULL')
           )

运行应用程序后,使用它来查看日志中的内容:

SELECT * FROM MyLog ORDER BY LogID

可以让您更好地了解SQL Server在查询中使用的实际参数。