“The active result contains no fields” using PDO with MS SQL

前端 未结 6 1273
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 02:29

I am in the process of converting some old PHP pages to use PDO.

Below are two simplified queries (not my actual queries) to aid understanding of the problem I\'m ha

相关标签:
6条回答
  • 2020-12-10 02:50

    If you are using a stored procedure then use

    SET NOCOUNT ON 
    

    The problem is that the stored procedure returns a result containing the number of rows affected as the first result.

    Microsoft Documentation

    0 讨论(0)
  • 2020-12-10 02:53

    The PDO engine sees this query as returning two result sets (the older mssql engine probably just ignored all but the last query in an overall query string). I have managed to make it work by skipping over the first result set (the temporary table) using the following command

    $statement->nextRowset();
    

    And then using $statement->fetch(); as normal

    0 讨论(0)
  • 2020-12-10 02:56

    If you are using a StoredProcedure and doing something as:

    DB::select("EXEC [storedprocedure] $param1,$param2;");
    

    As per above, the PDO expects the DB::select statement to return some data. But as your StoredProcedure does not return any data, you can change DB::select TO DB::update as belows:

    DB::update("EXEC [storedprocedure] $param1,$param2;");
    

    After this the error should no longer appear.

    0 讨论(0)
  • 2020-12-10 02:57

    I ran into this issue and the above answers helped, but the answers here assume things:

    1. You have access to modfy the sproc with SET NOCOUNT ON and you want to do this
    2. You're sure you need to move to the next rowset $statement->nextRowset();

    In my case I am using a generic method to pull back data from multiple sprocs where I may or may not have this issue and needed to check before incrementing the rowset.

    I used the following:

    while($stmt->columnCount() === 0 && $stmt->nextRowset()) {
        // Advance rowset until we get to a rowset with data
    }
    
    if($stmt->columnCount() > 0) {
        // We found something with data, do stuff.
        // Code here
    }
    

    Hopefully this helps someone else running into a similar issue.

    0 讨论(0)
  • 2020-12-10 02:57

    I am using Laravel5 in this version you should execute

    DB::select('SET NOCOUNT ON; EXEC stored_procedure'. $param1.','...$paramN);
    

    It worked perfect.

    0 讨论(0)
  • 2020-12-10 03:01

    I am using laravel5 and this is how I fixed the issue;

    DB::select("SET ANSI_NULLS ON; SET ANSI_WARNINGS ON;EXEC [storedprocedure] $param1,$param2;");
    
    0 讨论(0)
提交回复
热议问题