Show all database files on a php page?

0

Well, I made this code to display all the files that were attached by the user, however it only shows the file with the smallest ID, ie the first files in the database. This is the code I used:

<div class="container">
  <div class="text-center">
       <h1 class="tittlenoticia" style="color: black;">Arquivos anexados</h1>
       <?php
       require_once('conecta.php');
       $pasta = "uploads/";
       $consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
        var_dump($consulta);
       if ($resultado = mysqli_fetch_array($consulta)) {
          do {
            echo "<a href=\"" . $pasta . $resultado["nomearq"] . "\">" . $resultado["nomearq"] . "</a><br />";
          }
          while ($resultado = mysql_fetch_array($consulta));
        } 
       ?>

  </div>
</div>

I made the var_dump of the variable $consulta and the following appeared:

object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(9) ["type"]=> int(0) }

Does anyone have any idea why it does not show the other files? And one more thing, this php code only runs if I send a file, but I wanted it to run at the start of the page, if you have any ideas I'm grateful too!

    
asked by anonymous 25.10.2017 / 15:15

1 answer

1

Your error is occurring because of a typo.

See, to open the query, you are using:

if ($resultado = mysqli_fetch_array($consulta)) {

However, in do/while , you are using

while ($resultado = mysql_fetch_array($consulta));

You are mixing mysqli with mysql_* functions. You should only use mysqli_fetch_array .

To simplify things, do while direct, you will not have problems:

while ($resultado = mysqli_fetch_array($consulta)) {
    echo "<a href=\"" . $pasta . $resultado["nomearq"] . "\">" . $resultado["nomearq"] . "</a><br />";
} 

Moreover, in its if there is no else .

    
25.10.2017 / 15:50