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.

  • What is the best way to fetch records batch-wise from SQL Server
  • Function in SQL Server 2008 similar to GREATEST in mysql?
  • Locale error with umlauts in SSMS? Collation error?
  • case statement in SQL, how to return multiple variables?
  • Code to Insert Image into SQL Server
  • How to add sequence number for groups (new number if same group occurs again) in an SQL query
  • $pdo->prepare("SELECT x FROM y", array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));

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

    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.