Bank data comes incomplete

1

Good afternoon guys,

I have two data in the database, but making the select only 1 appears, why?

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$opcao = $_GET['opcao'];

switch ($opcao) {
    case 'pegaCategoria':

    $pegaCategoria=$pdo->prepare("SELECT * FROM categoriaComum");
    $pegaCategoria->execute();

    $return = array();

    while ($linhaCateAdmin=$pegaCategoria->fetch(PDO::FETCH_ASSOC)) {

        $idcategoriaComum = $linhaCateAdmin['idcategoriaComum'];
        $categoriaComum = $linhaCateAdmin['categoriaComum'];

        $return = [
            "idcategoriaComum" => $idcategoriaComum,
            "categoriaComum" => $categoriaComum
        ];

    }

    echo json_encode($return);

    break;
?>

And from the image below, you can see that I have two data in the table.

Andintheconsole,only1isdisplayed.

    
asked by anonymous 20.12.2016 / 17:44

1 answer

4

So you're rewriting the value and you only end up with the last ones returned in the last loop, do this:

...
$return = array();
while ($linhaCateAdmin=$pegaCategoria->fetch(PDO::FETCH_ASSOC)) {

        $return[] = [
            "idcategoriaComum" => $linhaCateAdmin['idcategoriaComum'],
            "categoriaComum" => $linhaCateAdmin['categoriaComum']
        ];

}
echo json_encode($return);
break;
...
    
20.12.2016 / 17:47