Why is the second WHILE not working?

Asked

Viewed 122 times

2

$consulta = "SELECT * FROM `teste` WHERE id='1' ";
<div class="box-header">
          <h2><?php while($d = $con->fetch_array()){ ?>
                <?php echo $d["cnpj_cpf_tab_clientes"];?>
                <?php echo $d["telefone_tab_clientes"];?>                               
                <?php } ?></h2>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                <thead><tr>
                  <th>CNPJ</th>
                  <th>Telefone</th>                   
                  </tr>
                </thead>
                <tbody>
                <?php while($dado = $con->fetch_array()){ ?>
                            <tr>
                            <td><?php echo $dado["cnpj_cpf_tab_clientes"];?></td>
                            <td><?php echo $dado["telefone_tab_clientes"];?></td>
                            </tr>
                <?php } ?>
                </tbody>
                </table>
                <h2>Endereço</h2>
            </div>

Because the second while($dado..) no results return for me?

  • 1

    Because the resultset has already been exhausted in the first while (if they are the same)

  • They’re the same. You can help me how I can fix this?

  • 1

    You can do while 1x by adding the data to an array, and then later you can call them using a foreach in this new array

  • I get it, I’ll research how to use foreach, thanks

  • I made a test in my database and the way it’s in my answer is working round!

  • @Junior, if you want an example, I’ll make it for you

Show 1 more comment

1 answer

5


It turns out that fetch_array "consumes" all the lines in the first while, i.e., changes the internal pointer until the end of the data during while. This way you need to return the pointer to the first line by placing:

mysqli_data_seek($result, 0);

before the second while

   <div class="box-header">
      <h2>
      <?php 
      $result = $con->query($consulta);
      while($d = $result->fetch_array()){ ?>
            <?php echo $d["cnpj_cpf_tab_clientes"];?>
            <?php echo $d["telefone_tab_clientes"];?>                               
            <?php } ?></h2>
    </div>
    <!-- /.box-header -->
    <div class="box-body">
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
            <thead><tr>
              <th>CNPJ</th>
              <th>Telefone</th>                   
              </tr>
            </thead>
            <tbody>
            <?php 
            mysqli_data_seek($result, 0);
            while($dado = $result->fetch_array()){ ?>
                        <tr>
                        <td><?php echo $dado["cnpj_cpf_tab_clientes"];?></td>
                        <td><?php echo $dado["telefone_tab_clientes"];?></td>
                        </tr>
            <?php } ?>
            </tbody>
            </table>
            <h2>Endereço</h2>
        </div>
  • Thank you, thank you

Browser other questions tagged

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