title: Calling odbc_fetch_array after executing a stored procedure in PHP gives error [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
body: <p>So, I'm trying to use ODBC to execute a stored procedure in an SQL database, but it returns the error</p>
  
  <pre><code>odbc_fetch_array() [function.odbc-fetch-array]: 
      SQL error: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index, 
      SQL state S1002 in SQLGetData
  </code></pre>
  
  <p>Here's the PHP part, pretty standard</p>
  
  <pre><code>...
  $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);
  }
  </code></pre>
  
  <p>Here's the Stored Procedure, really small and easy</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here's the Table schema for AMENDMENTS</p>
  
  <pre><code>(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
  </code></pre>
  
  <p><strong>What's interesting is that it doesn't return the error if I remove the column <code>VER</code> from the procedure</strong></p>
  
  <p>Like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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 <code>VER</code>), and I've got stored procedures that don't.</p>
  
  <p>I've tried different methods of fetching the data in PHP using <code>odbc_prepare</code> and <code>odbc_execute</code> with both query structures, <code>{CALL usp_GetRelatedToID(?)}</code>, but that just gave me more errors.</p>
  
  <p>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.</p>
  
  <p><strong>Oh, and executing the plain (original) query instead of in a stored procedure gives no errors.</strong></p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>$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));
  </code></pre>
  
  <p>Both return this error message:</p>
  
  <pre><code>Warning: odbc_execute() [function.odbc-execute]: 
      SQL error: [Microsoft][ODBC SQL Server Driver]Cursor type changed, 
      SQL state 01S02 in SQLExecute
  </code></pre>