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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值