Getting the latest registration

0

I am using this code to get the data that is between the current time and time registered in bd

$sql = "SELECT * FROM acessos WHERE hora > :hora GROUP BY username";
$sql = $pdo->prepare($sql);
$sql->bindValue(":hora", date('H:i:s', strtotime("-1 minutes")));
$sql->execute();
$contagem = $sql->rowCount();

while($row = $sql->fetch()) {
    $teste = $row['username'].', ';
}

But the problem that is only getting the last registration, and I could not solve

    
asked by anonymous 04.09.2017 / 06:42

1 answer

1

Use the fetchAll function instead of fetch

$sql = 'SELECT * FROM acessos WHERE hora > :hora GROUP BY username';
$statement = $pdo->prepare($sql);
$statement->bindValue(":hora", date('H:i:s', strtotime('-1 minutes')));
$statement->execute();

$resultado = $statement->fetchAll(PDO::FETCH_ASSOC);

//Mostra resultados
var_dump($resultado);

References:

link link

    
04.09.2017 / 14:16