簡體   English   中英

PHP PDO Microsoft SQL Server:SQLSTATE [24000]:[Microsoft] [用於SQL Server的ODBC驅動程序11]-無效的游標狀態

[英]PHP PDO Microsoft SQL Server: SQLSTATE[24000]: [Microsoft][ODBC Driver 11 For SQL Sever] - Invalid Cursor State

我有這個php腳本,應該向系統添加新的運行。 每次運行它時,它都會告訴我我的光標狀態無效。 我不知道為什么會發生此錯誤。 在進行下一部分代碼之前,我已經進行了closeCursor()調用。 這是php腳本,它在第42行上拋出錯誤(我的CreatePersonalRun存儲過程的execute命令):

if (isset($emailAddress, $caloriesBurnt, $dateRecorded, $deleted, $endTime, $startTime, $runType, $routeId)) {

$getAthleteID = $dsn->prepare("{CALL sp_GetAthleteId(?)}");
$getAthleteID->bindParam(1, $emailAddress, PDO::PARAM_STR);
$getAthleteID->execute();
$athleteData = $getAthleteID->fetch(PDO::FETCH_ASSOC);
$athleteId = $athleteData["AthleteID"];
$getAthleteID->closeCursor();
$runType = 0;
$deleted = false;
if ($athleteId != null) {
    $getAthleteID->closeCursor();
    $addRun = $dsn->prepare("{CALL sp_CreatePersonalRun(?,?,?,?,?,?,?,?)}");
    $addRun->bindValue(1, $athleteId);
    $addRun->bindValue(2, $caloriesBurnt);
    $addRun->bindValue(3, $dateRecorded);
    $addRun->bindValue(4, $deleted);
    $addRun->bindValue(5, $endTime);
    $addRun->bindValue(6, $routeId);
    $addRun->bindValue(7, $startTime);
    $addRun->bindValue(8, $runType);
    $addRun->execute(); //This is where the error is thrown
    if ($addRun->rowCount() > 0) {
        echo json_encode(array("code" => 100, "message" => "Run has been added to the system successfully!"));
    } else {
        echo json_encode(array("code" => 200, "message" => "Something went wrong with adding the run to the system"));
    }

} else {
    echo json_encode(array("code" => 200, "message" => "Athlete does not exist"));
}

我的getAthleteId存儲過程的代碼:

ALTER PROCEDURE [dbo].[sp_GetAthleteId]

@emailAddress nvarchar(400)

AS

SELECT * FROM Athlete WHERE EmailAddress = @emailAddress;

形成此SOURCE原因以獲取Invalid cursor state錯誤,

通過SQLFetch或SQLFetchScroll將光標定位在StatementHandle上。 如果SQLFetch或SQLFetchScroll沒有返回SQL_NO_DATA,則驅動程序管理器將返回此錯誤;如果SQLFetch或SQLFetchScroll返回了SQL_NO_DATA,則驅動程序將返回此錯誤。

在StatementHandle上打開了一個游標。

與StatementHandle關聯的已准備語句包含一個定位的update或delete語句,並且光標位於結果集的開始之前或結果集的結束之后。

因此,請檢查存儲的過程是否返回任何結果,並嘗試將$getAthleteID->fetch(PDO::FETCH_ASSOC)替換為

$getAthleteID->fetchAll(PDO::FETCH_ASSOC);

我整個上午都在為此苦苦掙扎,直到找到答案:

確保已為存儲過程設置了SET NOCOUNT ON-所有這些行都顯示“(x個受影響的行)”只是混淆了PDO驅動程序的內容,並導致未創建結果集。

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM