dongtao4319 2011-11-04 15:16
浏览 170
已采纳

在PHP中执行存储过程后调用odbc_fetch_array会出现错误[Microsoft] [ODBC SQL Server驱动程序]无效的描述符索引

So, I'm trying to use ODBC to execute a stored procedure in an SQL database, but it returns the error

odbc_fetch_array() [function.odbc-fetch-array]: 
    SQL error: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index, 
    SQL state S1002 in SQLGetData

Here's the PHP part, pretty standard

...
$id = 240

$user = "user";
$password = "password";
$server = "server";
$database = "database";

$con = odbc_connect("SERVER=$server; 
                     DRIVER=SQL Server;
                     DATABASE=$database", 
                     $user, 
                     $password);    

$res = odbc_exec($con, "exec usp_GetRelatedToID '$id'");

while($row = odbc_fetch_array($res)){
    print_r($row);
}

Here's the Stored Procedure, really small and easy

CREATE PROCEDURE [dbo].[usp_GetRelatedToID]
    @id int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT AMENDMENT_ID, WDATE, ALTERATION, VER, REASON
    FROM AMENDMENTS
    WHERE AMENDMENT_ID = $id
END

Here's the Table schema for AMENDMENTS

(Column_name)      (Type)        (Nullable)
AMENDMENT_ID        int             no
RAD_MAIN_ID         int             yes
WDATE               datetime        yes
USR_ID              int             yes
ALTERATION          varchar         yes
REASON              varchar         yes
VER                 int             yes

Identity        Seed   Increment   Not For Replication
AMENDMENT_ID      1        1               0

constraint_type               constraint_name      constraint_keys
PRIMARY KEY (non-clustered) aaaaaAMENDMENTS1_PK      AMENDMENT_ID

What's interesting is that it doesn't return the error if I remove the column VER from the procedure

Like this:

CREATE PROCEDURE [dbo].[usp_GetRelatedToID]
    @id int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT AMENDMENT_ID, WDATE, ALTERATION, REASON
    FROM AMENDMENTS
    WHERE AMENDMENT_ID = $id
END

Can anyone explain where I'm doing wrong and why this keep happening? I've got other stored procedures that give the same errors (some also share the column VER), and I've got stored procedures that don't.

I've tried different methods of fetching the data in PHP using odbc_prepare and odbc_execute with both query structures, {CALL usp_GetRelatedToID(?)}, but that just gave me more errors.

And for reasons I won't go into, I can't use mssql functions in PHP, ODBC is the only way I'm allowed to connect and query.

Oh, and executing the plain (original) query instead of in a stored procedure gives no errors.

EDIT

$stmt = odbc_prepare($con, "{CALL usp_GetRelatedToID($id)}");
$res = odbc_execute($stmt, array());

//or

$stmt = odbc_prepare($con, "{CALL usp_GetRelatedToID(?)}");
$res = odbc_execute($stmt, array($id));

Both return this error message:

Warning: odbc_execute() [function.odbc-execute]: 
    SQL error: [Microsoft][ODBC SQL Server Driver]Cursor type changed, 
    SQL state 01S02 in SQLExecute
  • 写回答

1条回答 默认 最新

  • douwen8424 2012-06-25 19:56
    关注

    I ran into this problem as well. The way I got around it was to use

    odbc_exec($connection, $sql)
    

    Instead of

    odbc_execute($connection, $sql)
    

    Per a user comment here

    Comment was:

    BTW. If anyone is banging his head about "cursor type changed" warning while using execute with ORDER BY clause, then just use exec for now (remember to addslashes for yourself).

    In PHP 5.3 a Bug #43668 will be fixed and it will allow you to change a cursor type to SQL_CUR_USE_ODBC

    Note that you could also try to select a cursor type in odbc_connect, but that didn't work for me (much more problems appeared then it solved).

    Apparently this is a bug in PHP, and will be patched per this

    So try using odbc_exec() insted of odbc_execute() if you are having this problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab