Error when trying to list objects that are in the database when using Foreach ();

0

I am making this code to capture the elements of a table in my database but when I run the foreach code I get the error and I do not know what to do

Data fetch function:

function listaUniformes($conexao) {
$uniformes = array();
$query = "select * from uniformes";
$resultado = mysqli_query($conexao, $query);
while($uniforme = mysqli_fetch_assoc($resultado)) {
    array_push($uniformes, $uniforme);
}
return $uniforme;

}

Foreach:

<?php
                listaUniformes($conexao);
                 foreach($uniformes as $uniforme) {
                    ?>
                     <tr>
                         <?= $uniforme['nome']; ?>
                     </tr>
            <?php } ?>

Error when entering page

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\warehouse\uniformes.php on line 21
    
asked by anonymous 25.07.2017 / 19:48

1 answer

2

Your function returns the wrong value should return $uniformes and not $uniforme

Change:

return $uniforme;

To:

return $uniforme;

Remember to create a variable or pass an array to the foreach, you can do as follows:

<?php
   foreach(listaUniformes($conexao) as $uniforme) {

Or:

<?php
   $arr = listaUniformes($conexao);
   foreach($arr as $uniforme) {
    
25.07.2017 / 20:07