简体   繁体   中英

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

I have this php script which is supposed to add a new run to the system. Each time I run it, it tells me I have an invalid cursor state. I cannot figure out why this error is happening. I have made my closeCursor() calls before moving on to the next bit of code. This is the php script, it throws the error on line 42 (the execute command for my CreatePersonalRun stored procedure):

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"));
}

Code for my getAthleteId stored procedure:

ALTER PROCEDURE [dbo].[sp_GetAthleteId]

@emailAddress nvarchar(400)

AS

SELECT * FROM Athlete WHERE EmailAddress = @emailAddress;

Form this SOURCE reason to get Invalid cursor state error,

A cursor was positioned on the StatementHandle by SQLFetch or SQLFetchScroll. This error is returned by the Driver Manager if SQLFetch or SQLFetchScroll has not returned SQL_NO_DATA, and is returned by the driver if SQLFetch or SQLFetchScroll has returned SQL_NO_DATA.

A cursor was open on the StatementHandle.

The prepared statement associated with the StatementHandle contained a positioned update or delete statement, and the cursor was positioned before the start of the result set or after the end of the result set.

So check if the stored procdure is returning any result, and also try replace $getAthleteID->fetch(PDO::FETCH_ASSOC) with

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

I struggled with this all morning until I found the answer:

Make sure you have SET NOCOUNT ON for your stored procedure - all of those lines where it says "(x rows affected)" just confuse the heck out of the PDO driver and results in it not creating the result set(s).

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM