Show all database files on a php page?

Asked

Viewed 153 times

0

Guys, I made this code to display all the files that were attached by the user, but it only shows the file with the smallest ID, IE, the first files in the database. That’s 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 var_dump 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 he won’t show the other files? And one more thing, this php code only runs if I send a file, but I wanted it to run when starting the page, if you have any ideas I’m also grateful!

  • If you run the query directly in DBMS, it will return all user files?

  • @Gabrielheming Yes, returns all files!

1 answer

1


Your error is occurring because of a "typo".

See, to open the query, is using:

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

Meanwhile, in the do/while, is using

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

Is mixing mysqli serving mysql_*. Should only use mysqli_fetch_array.

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

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

Besides, in your if there is no else.

  • Dude, that was really it, I started using mysqli now, always forget that i. And it has some problem if leave the if without Else?

  • @Arthuroliveira No problem, but the code is redundant, IE, is writing more code to have the same result.

  • Understood, and do you have any idea how to make this code run when starting the page? Because it only runs when I attach a file and click send.

  • @Arthuroliveira how so? In theory, if the page is being called, it already runs automatically. However, I believe that your question should be asked as another question.

  • That’s what I thought, too, but he only executes if he attaches a file. I’m going to ask this question as another question so thank you so much for your help, thank you so much!!

Browser other questions tagged

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