How to do while in PDO and data manipulation?

Asked

Viewed 84 times

0

I need to do a while with PDO and pick up each dice, manipulate them and play within an array. How can I do that?

My php:

while ($linhaMsg=$pegaMsgsLogra->fetch(PDO::FETCH_ASSOC)) {
    @$idAviso = $linhaMsg['idAvisoLogradouro'];
    @$idUsuario = $linhaMsg['idUsuario'];
    @$msg = $linhaMsg['msg'];
    @$foto = utf8_encode($linhaMsg['foto']);
    @$hora = $linhaMsg['hora'];

    $horaP = explode(':', $hora);
    $hora = $horaP[0].':'.$horaP[1];

    $return = array(
            'nome' => $nome,
            'msg' => $msg,
            'foto' => $foto,
            'hora' => $hora
        );

}

1 answer

2


Just add a [] in the $return:

$return = array(); // Nem precisa desta linha, mas no mínimo fica mais fácil entender.

while ($linhaMsg=$pegaMsgsLogra->fetch(PDO::FETCH_ASSOC)) {
    @$idAviso = $linhaMsg['idAvisoLogradouro'];
    @$idUsuario = $linhaMsg['idUsuario'];
    @$msg = $linhaMsg['msg'];
    @$foto = utf8_encode($linhaMsg['foto']);
    @$hora = $linhaMsg['hora'];

    $horaP = explode(':', $hora);
    $hora = $horaP[0].':'.$horaP[1];

    $return[] = array(
        'nome' => $nome,
        'msg' => $msg,
        'foto' => $foto,
        'hora' => $hora
    );

}

In this way, $return it’s going to be a array with others arrays within.

The first name will be on $return[0]['nome'], the second in $return[1]['nome'], and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.