Php 在更新之后选择“不工作”

Php 在更新之后选择“不工作”,php,sql,Php,Sql,我正在使用PHP向Sql Server发送查询。问题是,如果在UPDATE语句之后执行“SELECT 1 AS updateResults”语句,则该语句不起作用。资源返回到PHP,但它没有任何行 以下是我的查询的简化版本: --SELECT 1 AS updateResults --if this is done before the UPDATE, a row gets returned UPDATE theTable SET theValue = 'x' WHERE theRow = '

我正在使用PHP向Sql Server发送查询。问题是,如果在UPDATE语句之后执行“SELECT 1 AS updateResults”语句,则该语句不起作用。资源返回到PHP,但它没有任何行

以下是我的查询的简化版本:

--SELECT 1 AS updateResults --if this is done before the UPDATE, a row gets returned

UPDATE theTable SET theValue = 'x' WHERE theRow = '5'

SELECT 1 AS updateResults --if this is done after the UPDATE, a row doesn't get returned
我的更新查询是有效的,并且执行它应该执行的操作

我还尝试了“SELECT'updateResults'=1”格式,并尝试放置一个;在每一句话的结尾,都没有什么不同

有什么想法吗

以下是PHP代码:

$updateSQL = "BEGIN TRANSACTION

DECLARE @result1 int
DECLARE @result2 int

--SELECT 1 AS updateResults --if this is here, the row gets returned

UPDATE theTable
    SET endDate = '" . $endDate . "'
    WHERE permitYear = '" . $permitYear . "'

UPDATE theTable
    SET startDate = '" . $startDate . "'
    WHERE permitYear = '" . $nextYear . "'

--Test to make sure both records were saved
SELECT @result1 = permitYear
    FROM theTable
    WHERE permitYear = '" . $permitYear . "'
    AND endDate = '" . $endDate . "'

SELECT @result2 = permitYear
    FROM theTable
    WHERE permitYear = '" . $nextYear . "'
    AND startDate = '" . $startDate . "'

if ((@result1 > 0) AND (@result2 > 0))
    BEGIN
        COMMIT TRANSACTION
        SELECT 1 AS updateResults
    END
ELSE
    BEGIN
        ROLLBACK TRANSACTION
        SELECT 0 AS updateResults
    END";

$updateRS = sqlsrv_query($con, $updateSQL);

if (!is_resource($updateRS)) {
    //There was a problem. A resource wasn't returned. It never fails here
    exit;
}

if (!sqlsrv_has_rows($updateRS)) {
    echo ("fail for no rows returned");
    exit;
}

$updateARR = sqlsrv_fetch_array($updateRS);

//It makes it here if the SELECT is done before the UPDATE
if ($updateARR['updateResults'] == '1') {
    //success
}
else {
    //save problem
}

附加信息:如果我取出2条UPDATE语句,它将按预期运行并返回updateResults行。

您应该像这样进行查询选择

SELECT * FROM table1 WHERE 'updateResults' = 1

我最终在两条SQL语句中完成了这项工作,而不是一条。以下是我所做的:

我从$updateSQL中取出SELECT x AS updateResults行,然后:

$updateSQL = ...(same as the question, without the SELECT x AS updateResults lines)...

sqlsrv_query($con, $updateSQL);

$testSQL = "
DECLARE @result1 int
DECLARE @result2 int

SELECT @result1 = permitYear
FROM theTable
WHERE permitYear = '" . $permitYear . "'
    AND endDate = '" . $endDate . "'

SELECT @result2 = permitYear
FROM Transportation.dbo.PermitDate
WHERE permitYear = '" . $nextYear . "'
    AND startDate = '" . $startDate . "'

if ((@result1 > 0) AND (@result2 > 0))
    BEGIN
        SELECT 1 AS updateResults
    END
ELSE
    BEGIN
        SELECT 0 AS updateResults
    END
";

$testRS = sqlsrv_query($con, $testSQL);

if (!is_resource($testRS)) {
    //There was a problem. A resource wasn't returned
}

if (!sqlsrv_has_rows($testRS)) {
    //fail for no rows
}

$testARR = sqlsrv_fetch_array($testRS);

if ($testARR['updateResults'] == '1') {
    //success
}
else {
    //fail - the changes were not saved
}

当SELECT和UPDATE语句作为事务的一部分时,您是否尝试颠倒它们的顺序?如果没有,我会尝试:

而不是:

COMMIT TRANSACTION
SELECT 1 AS updateResults
尝试:

我没有“setnocounton”,因此在SELECT结果之前返回了诸如“(1行受影响)”之类的SQL消息。我的代码应该是:

$updateSQL = "SET NOCOUNT ON
    BEGIN TRANSACTION
    ...
这样就可以抑制SQL消息

作为补充说明,在我解决了这个问题后,我注意到以下几点:尽管我没有为此使用存储过程,但我注意到,当您在SQL Server Management Studio中创建存储过程时,它甚至会将以下内容放入模板中:

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

可能是愚蠢的问题,但是你有正确的PHP命令来执行多个语句吗?请显示代码。这是查询字符串。。。。但是我们能看到实际执行该查询的代码吗?为什么要检查两条记录是否都插入了?事务的全部要点是,如果其中一个插入失败,它将回滚(即,只有当两个插入都成功完成时,它才会提交)。基于成功或失败,我的PHP代码执行不同的操作。我可以让PHP在运行之后再做一次测试,但那是另一个查询和更多的网络流量。我不建议在“updateResults”周围加引号,除非您希望它被视为字符串文字。我不希望从表中返回值。选择1(或0)作为我想要的updateResults。
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;