我有这个php脚本,应该添加一个新的运行到系统。每次运行它时,它都会告诉我光标状态无效。我不知道为什么会发生这个错误。在进入下一段代码之前,我已经进行了closeCursor()调用。这是php脚本,它在第42行(CreatePersonalRun存储过程的执行命令)抛出错误:
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 reason获取Invalid cursor state
error,
通过SQLFetch或SQLFetchScroll将游标定位在StatementHandle上。如果SQLFetch或SQLFetchScroll没有返回SQL_NO_DATA,则由驱动程序返回此错误;如果SQLFetch或SQLFetchScroll返回SQL_NO_DATA,则由驱动程序返回此错误。
在StatementHandle上打开了一个游标。
与StatementHandle相关联的预处理语句包含一个定位的更新或删除语句,游标定位在结果集开始之前或结束之后。
因此检查存储过程是否返回任何结果,并尝试替换$getAthleteID->fetch(PDO::FETCH_ASSOC)
$getAthleteID->fetchAll(PDO::FETCH_ASSOC);
我整个上午都在纠结这个问题,直到我找到了答案:
确保您为存储过程设置了SET NOCOUNT ON—所有那些说"(x行受影响)"的行只会混淆PDO驱动程序,并导致它没有创建结果集。
HTH