sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo puedo usar PDO para obtener una matriz de resultados en PHP?

Eche un vistazo a PDOStatement.fetchAll método. También puede usar fetch en un patrón iterador.

Ejemplo de código para fetchAll , de la documentación de PHP:

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
print_r($result);

Resultados:

Array
(
    [0] => Array
        (
            [NAME] => pear
            [COLOUR] => green
        )

    [1] => Array
        (
            [NAME] => watermelon
            [COLOUR] => pink
        )
)