select with pdo in functions

1

How do I use my select that is in my funções.php in this table that is in my estoque.php , I already did the script I just can not call the $resultado that is in the function to use in foreach .

STOCK.PHP         

    selecionar_produtos();

?>  

<div class="container">
    <div class="table-responsive">
    <table class="table">
        <thead>

            <tr>

                <th>Nome</th>
                <th>Marca</th>
                <th>Modelo</th>
                <th>Quantidade</th>
                <th>Estado</th>
                <th>Opções</th>

            </tr>

        </thead>

        <tbody>

        <?php foreach ($resultado as $key) :?>

            <tr>

                <td><?php echo $key->nome;?></td>
                <td><?php echo $key->marca;?></td>
                <td><?php echo $key->modelo;?></td>
                <td><?php echo $key->quantidade;?></td>
                <td><?php echo $key->estado;?></td>
                <td>
                    <a href="mysql/update.php?id=<?php echo $key->id; ?>" class="btn btn-info">Editar</a>

                    <a href="mysql/delete.php?id=<?php echo $key->id; ?>" class="btn btn-danger">Deletar</a>

                </td>

            </tr>

            <?php endforeach;?>

FUNCOES.PHP

    <?php

    function selecionar_produtos(){

    require "mysql/pdo.php";

    try{

        $selecionar = "select * from estoqueprodutos . produtos";

        $stmt = $pdo->prepare($selecionar);

        $stmt->execute();

        $resultado = $stmt->fetchAll(PDO::FETCH_OBJ);


    } catch (PDOException $error){
        echo "Erro:\n" . $error->getMessage();  
    }
    }
    ?>
    
asked by anonymous 23.12.2015 / 03:18

1 answer

3

Your function returns nothing as soon as you can not receive anything in the function call, add a return .

$resultado = $stmt->fetchAll(PDO::FETCH_OBJ);
return $resultado;

With the function returning the records of the finger bank make an assignment, since the variable $resultado exists within the function selecionar_produtos() after its call it does not exist.

$produtos = selecionar_produtos();
//código omitido
<?php foreach ($produtos as $key) :?>
    
23.12.2015 / 03:45