Problems with MYSQL data return via include in PHP

Asked

Viewed 59 times

2

I am starting a code for a warehouse control using PHP and MYSQL, and when making a document of function separated from index.php and perform proper variable insertions via include duly stated in the head index.php and call the variable $columns, this does not return all the contents of my table in MYSQL, only repeats several times the first row of the table referred.

Follow 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>
  • Put include before doctype

  • I put it but it didn’t work any other way.

  • First, why are you creating a document html no include?

  • Oops! Even with HTML body the document is php, but as it was the first time I posted something here and I have no experience in posting code, I was not able to put only php of funcoes_almox.php and as I saw that the code of index.php entered, put equal.

1 answer

0


Substitution while($colunas): for 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 if while goes through the entire query result).

  • It worked perfectly! But let me ask you another question. How to use as a function where I just insert only the content of variables and it returns the value, so I can repeat the function in several situations?

  • I don’t understand what you mean. Can you show some example? By the way, if the answer solved your problem, don’t forget to mark it as accepted ;)

  • Oops! How do I accept the answer? So, I think I expressed myself badly. I want to know how I can put this php code that reads the contents of the database as a function, that when I call it and pass the arguments, it can return me the content without having to declare the function several times in the php documents of the same site project.

  • Try to use $selecao_geral->fetchAll() out of the while and it should run. About the reply, follow link with clarifications: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply

Browser other questions tagged

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