Problems with MYSQL data return via include in PHP [closed]

2

I'm starting a code for a warehouse control using PHP and MYSQL, and by making a function document separate from index.php and performing the appropriate variable insertions via include declared in the head of index.php and call the variable $ columns , it does not return all the contents of my table in MYSQL, it just repeats the first line several times of the referenced table.

Here are the codes:

<!DOCTYPE html>

<html>
    <head lang="pt-br">
        <meta charset="UTF-8"/>
        <link rel="stylesheet" href="css/estilo_alm.css"/>
        <link onscroll="">
        <title>index.php</title>
        <?php include 'funcoes.php'; ?>
    </head>

    <body id="corpo">
        <header id="cabecalho">
            <h1>Projeto Almoxarifado ADVEC</h1>
            <nav id="menuPags">
                <ul type="disc">
                    <li><a class="pags" href="index.php">controle de material</a></li>
                    <li><a class="pags" href="controle_material.php">pedido de material</a></li>
                    <li><a class="pags" href="controle_notas.php">controle de notas</a></li>
                    <li><a class="pags" href="saida_material.php">saída de material</a></li>
                </ul>
            </nav>
        </header>
        <section id="titulo_principal">
            <nav id="menuTabela">
                <ul type="disc">
                    <li><a class="tabela" href="index.php">controle de material</a></li>
                    <li><a class="tabela" href="controle_material.php">pedido de material</a></li>
                    <li><a class="tabela" href="controle_notas.php">controle de notas</a></li>
                    <li><a class="tabela" href="saida_material.php">saída de material</a></li>
                </ul>
            </nav>
            <div id="principal">
                <table id="tabela2">
                    <?php
                        while($colunas): ?>
                            <tr>
                                <td id="td"><?php echo utf8_encode($colunas['idcidade']); ?></td>
                                <td id="td1"><?php echo utf8_encode($colunas['cidade']); ?></td>
                                <td id="td2"><?php echo utf8_encode($colunas['estado']); ?></td>
                            </tr>
                    <?php endwhile; ?>
                </table>
            </div>
        </section>
        <aside id="menuBotoes">
            <ul type="disc">
                <li><a class="botoes" href="index.php">controle de material</a></li>
                <li><a class="botoes" href="controle_material.php">pedido de material</a></li>
                <li><a class="botoes" href="controle_notas.php">controle de notas</a></li>
                <li><a class="botoes" href="saida_material.php">saída de material</a></li>
            </ul>
        </aside>
        <footer id="rodape">
        </footer>
        <?php $conecta->close(); ?>
    </body>
</html>


<!DOCTYPE html>

<html>
<head lang="pt-br">
    <meta charset="UTF-8"/>
    <title>funcoes_almox.php</title>
</head>

<body>
<?php
    define("SERVIDOR", "localhost");
    define("USUARIO", "root");
    define("SENHA", "");
    define("BANCODEDADOS", "controle_almoxarifado_v02");

    $conecta = new mysqli(SERVIDOR, USUARIO, SENHA, BANCODEDADOS);
    if ($conecta->connect_error) {
        trigger_error("ERRO NA CONEXÃO: " . $conecta->connect_error, E_USER_ERROR);
    }

    $selecao_geral = mysqli_query($conecta, 'select * from cidade');
    $colunas = $selecao_geral->fetch_assoc();
?>
</body>
</html>
    
asked by anonymous 20.09.2018 / 19:06

1 answer

0

Replaced while($colunas): with while($colunas = $selecao_geral->fetch_assoc()): to update the value of $ columns for each interaction. Fetch_assoc () moves the pointer in the query result on each call to the function, and you need to update the value of $ columns, otherwise it will always be saved with the first value found (even though the while traverses the entire query result ).

    
20.09.2018 / 19:55