Doubt with variables and display in the list with PHP

Asked

Viewed 57 times

0

I wanted to know how I could display this data:

<?php 
// session_start();
include("conexao.php");//chama o arquivo que faz a conexão com o banco 
 $sql = mysqli_query($conn, "select * from usuarios order by nome asc");//"joga" a query na variavel sql

 //a variavel exibe é para exibir os dados da lista do banco
 while($exibe = mysqli_fetch_assoc($sql)){

 echo $exibe["id"];
 echo $exibe["nome"];
 echo $exibe["email"];
 echo $exibe["senha"];
 echo $exibe["telefone"];

 }
?>

Which are being displayed with echo for a list on Html5:

<form method="POST"action="lista.php">
<table class="table table-dark">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Nome</th>
      <th scope="col">E-mail</th>
      <th scope="col">Telefone</th>
    </tr>

  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>     
  </tbody>
</table>
</form>

I did the test with echo to see if it would work and yes I can pull the items from my database, in case, phpMyAdmin.

1 answer

4

If I understand correctly you want to display the data returned from the database in your table at html, so I think the code below might help you.

<?php 
//chama o arquivo que faz a conexão com o banco 
include("conexao.php");

//"joga" a query na variavel sql
$sql = mysqli_query($conn, "select * from usuarios order by nome asc");
?>

<table class="table table-dark">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Nome</th>
      <th scope="col">E-mail</th>
      <th scope="col">Telefone</th>
    </tr>
  </thead>

  <tbody>
    <?php while($data = mysqli_fetch_assoc($sql)): ?>
      <tr>
          <th scope="row"><?php echo $data["id"]; ?></th>
          <td><?php echo $data["nome"]; ?></td>
          <td><?php echo $data["email"]; ?></td>
          <td><?php echo $data["telefone"]; ?></td>
      </tr>     
    <?php endwhile; ?>
  </tbody>
</table>
  • while Kayo was open

  • True, thank you @gmsantos.

  • I’m sorry but I didn’t understand why the $data variable ? it was created at that time so that ?

  • Notice: Undefined variable: sql in C: xampp htdocs testephp listaPage.php on line 93 Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs testephp listaphp Page.php on line 93 These lines appear when I try to run the project after putting its code @Kayobruno

  • @Marcelol, this is just the name chosen to capture the return, instead of $data could still be the one you created, $exibe hassle-free

  • 1

    @Marcelol, about the error, checks if you have this database query line (select *...) with this variable name, $sql

  • $result_usuarios = "SELECT * FROM usuarios"; $resultado_usuarios = mysqli_query($Conn, $result_usuarios); this is my "list.php" where I connect to the database and call it at the beginning of the list

  • @Dimaspante forgot to mark kkk

  • @Marcelol, tranquil. So, just you change the variable inside that mysqli_fetch_assoc, from $sqlfor $result_usuarios

  • @Dimaspante ai que ta...ja testei e da isso aqui Notice: Undefined variable: result_usuarios in C: xampp htdocs testephp listaPage.php on line 106

Show 6 more comments

Browser other questions tagged

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