Export (0) Print
Expand All

PDOStatement::fetchAll

Returns the rows in a result set in an array.


array PDOStatement::fetchAll([ $fetch_style[, $column_index ][, ctor_args]] );

$fetch_style: An (integer) symbol specifying the format of the row data. See PDOStatement::fetch for a list of values. PDO::FETCH_COLUMN is also allowed. PDO::FETCH_BOTH is the default.

$column_index: An integer value representing the column to return if $fetch_style is PDO::FETCH_COLUMN. 0 is the default.

$ctor_args: An array of the parameters for a class constructor, when $fetch_style is PDO::FETCH_CLASS or PDO::FETCH_OBJ.

An array of the remaining rows in the result set, or false if the method call fails.

Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

<?php
   $server = "(local)";
   $database = "AdventureWorks";
   $conn = new PDO( "sqlsrv:server=$server ; Database = $database", "", "");

   print "-----------\n";
   $stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
   $result = $stmt->fetchall(PDO::FETCH_BOTH);
   print_r( $result );
   print "\n-----------\n";

   print "-----------\n";
   $stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
   $result = $stmt->fetchall(PDO::FETCH_NUM);
   print_r( $result );
   print "\n-----------\n";

   $stmt = $conn->query( "select * from Person.ContactType where ContactTypeID < 5 " );
   $result = $stmt->fetchall(PDO::FETCH_COLUMN, 1);
   print_r( $result );
   print "\n-----------\n";

   class cc {
      function __construct( $arg ) {
         echo "$arg\n";
      }

      function __toString() {
         echo "To string\n";
      }
   };

   $stmt = $conn->query( 'SELECT TOP(2) * FROM Person.ContactType' );
   $all = $stmt->fetchAll( PDO::FETCH_CLASS, 'cc', array( 'Hi!' ));
   var_dump( $all );
?>

Reference

Other Resources

PDO

Community Additions

Show:
© 2015 Microsoft