简体   繁体   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

I have this php script which is supposed to add a new run to the system. 我有这个php脚本,应该向系统添加新的运行。 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. 在进行下一部分代码之前,我已经进行了closeCursor()调用。 This is the php script, it throws the error on line 42 (the execute command for my CreatePersonalRun stored procedure): 这是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"));
}

Code for my getAthleteId stored procedure: 我的getAthleteId存储过程的代码:

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, 形成此SOURCE原因以获取Invalid cursor state错误,

A cursor was positioned on the StatementHandle by SQLFetch or SQLFetchScroll. 通过SQLFetch或SQLFetchScroll将光标定位在StatementHandle上。 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. 如果SQLFetch或SQLFetchScroll没有返回SQL_NO_DATA,则驱动程序管理器将返回此错误;如果SQLFetch或SQLFetchScroll返回了SQL_NO_DATA,则驱动程序将返回此错误。

A cursor was open on the StatementHandle. 在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. 与StatementHandle关联的已准备语句包含一个定位的update或delete语句,并且光标位于结果集的开始之前或结果集的结束之后。

So check if the stored procdure is returning any result, and also try replace $getAthleteID->fetch(PDO::FETCH_ASSOC) with 因此,请检查存储的过程是否返回任何结果,并尝试将$getAthleteID->fetch(PDO::FETCH_ASSOC)替换为

$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). 确保已为存储过程设置了SET NOCOUNT ON-所有这些行都显示“(x个受影响的行)”只是混淆了PDO驱动程序的内容,并导致未创建结果集。

HTH 高温超导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此扩展需要PHP中用于SQL Server的Microsoft ODBC Driver 11 - This extension requires the Microsoft ODBC Driver 11 for SQL Server in PHP SQLSTATE [HY000]:[Microsoft] [SQL Server的ODBC驱动程序11] TDS协议错误 - SQLSTATE[HY000]: [Microsoft][ODBC Driver 11 for SQL Server]TDS Protocol error 带PHP的RedHat Linux上用于SQLServer®的Microsoft®ODBC驱动程序11-与PDO的存储过程绑定参数时出现错误 - Microsoft® ODBC Driver 11 for SQL Server® on RedHat Linux with PHP - gives an error when bind parameter with PDO for stored procedures 无效的游标状态,SQLExecDirect 中的 SQL 状态为 24000 - Invalid cursor state, SQL state 24000 in SQLExecDirect 无效的游标状态-pdo_odbc FreeTDS SQL Server - Invalid cursor state - pdo_odbc FreeTDS SQL Server 用于Microsoft SQL Server的PHP ODBC PDO驱动程序返回空数组或false - PHP ODBC PDO driver for Microsoft SQL Server returning empty array or false 如何使用PHP在RedHat Linux上为SQLServer®配置Microsoft®ODBC驱动程序11 - How to Configure Microsoft® ODBC Driver 11 for SQL Server® on RedHat Linux with PHP 42000 [SQLSTATE] [Microsoft] [用于 SQL 服务器的 ODBC 17 驱动程序] [SQL Server] 无法打开数据库“ - 42000 [SQLSTATE] [Microsoft] [ODBC 17 driver for SQL Server] [SQL Server] It is not possible to open the database " SQLSTATE [42000]:[Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]'`'附近的语法不正确 - SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near '`' SQLSTATE[28000]:[Microsoft][SQL 服务器的 ODBC 驱动程序 17][SQL Server] 用户登录失败 - SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM