Is there a way to set a default cursor type in PDO (specifically PDO_SQLSRV)?

So, annoyingly, the PDO_SQLSRV driver from Microsoft returns -1 on any rowCount() call from a SELECT query with non-scrollable cursors.

Further adding to the annoyance, the default cursor type is _FORWARD, meaning that I am seemingly having to add attributes to every prepared statement where I may need to get a row count back (in this app, that’s A LOT) as below. This is unacceptable, as some of the database engines we will be using do not support scrollable cursors.

  • How can I optimally filter a view from a stored procedure that accepts lots of nullable parameters?
  • Query times out when executed from web, but super-fast when executed from SSMS
  • How to configure Passportjs with SQL Server?
  • Getting following error “Value does not fall within the expected range” while deploying the SSRS report to Sharepoint 2013 site
  • Store Procedure Result to Text file using SSIS package
  • What are the differences between T-SQL, SQL Server and SQL
  • $pdo->prepare("SELECT x FROM y", array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));
    

    Curiously this does not seem to work with PDO::query()…

    Edit:
    As below, I’ve also tried PDO::setAttribute() and adding the option to PDO::__construct(), but neither throw exceptions or errors, or seem to actually do anything!

    My question to you all is: is there a way to set the default cursor type to CURSOR_SCROLL?

    One Solution collect form web for “Is there a way to set a default cursor type in PDO (specifically PDO_SQLSRV)?”

    What about:

    $pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL);
    

    ?

    Also you can extend base PDO class with your own, with prepare method overriden:

    class MyPdo extends PDO
    {
        public function prepare($statement, $options = array())
        {
            if (empty($options)) $options = array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
            return parent::prepare($statement, $options);
        }
    }
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.