How to display the result of a query in a html page in PHP?

9

How can I display the results of a query in a one-page html table?

Below is what I was able to do. However, the code only shows the first record.

include("conectar.php");
$sql = mysql_query("Select * From tb_trabalhador and tb_detalhe_trabalhador");
$exibe = mysql_fetch_assoc($sql);
echo "<table>"; 
echo  "<tr><td>Nome:</td>";
echo "<td>".$exibe["Nome"]."</td></tr>"; e os outros campos
    
asked by anonymous 31.01.2014 / 17:53

5 answers

10

When more than one result is returned by your query, use while to get all of them.

$sql = mysql_query("Select * From tb_trabalhador and tb_detalhe_trabalhador");
while($exibe = mysql_fetch_assoc($sql)){
  echo $exibe['nome'] .'<br>';
}

I recommend using the mysql_error () to debug your code and get a bank error if this is a legacy project.

mysql_query($sql) or die(mysql_error());

It's highly recommended to use the PDO or mysqli to connect to the database, since mysql_* functions have already been deprecated and will soon be removed.

Reasons not to use the mysql functions _ *

    
31.01.2014 / 17:55
2

All mysql_fetch_ * functions return a single line and advance the internal cursor to the next record.

To get all the records, you need to use them within some repeat structure.

Example:

while ($exibe = mysql_fetch_assoc($sql) ) { // Obtém os dados da linha atual e avança para o próximo registro
  echo $exibe["nome"];
}

As mentioned, consider using a PDO extension to manage communication between application

31.01.2014 / 18:22
2

To get all the records you need a loopback:

include("conectar.php");
$sql = mysql_query("Select * From tb_trabalhador and tb_detalhe_trabalhador");
echo "<table>";
while($exibe = mysql_fetch_assoc($sql)){
   echo "<tr><td>Nome:</td>";
   echo "<td>".$exibe["Nome"]."</td></tr>";
}
echo "</table>";
    
04.02.2014 / 14:12
0

It is important to note that queries should not be executed in a view. The correct thing is that you apply an MVC (Model View Controller) pattern and separate things. A good example, using the Framework Code Igniter, would be:

model.php
function carregar_trabalhador(){
$banco = new mysqli("servidor","usuário","senha");
$sql="Select * From tb_trabalhador and tb_detalhe_trabalhador";
$resultadoConsulta=$banco->query($sql);
$trabalhadores = array();
$i=0;
        while (!empty($resultadoConsulta[$i])) {
            $row = $resultadoConsulta[$i]=
            $trabalhadores[] = $row;
            $i++;
        }

return $resultadoConsulta
}

controller.php 
function exibir_trabalhador(){
$model= new Model();
$trabalhadores = $model->carregar_trabalhador(); 
$this->load->view("trabalhador.html", $trabalhadores);  
}

trabalhador.html
<?php
foreach ($trabalhadores as $trabalhador) {
?>
<table> 
    <tr><td>Nome:</td> 
        <td><?= $trabalhador["Nome"] ?></td></tr>; e os outros campos
    <?php }
?>
    
07.02.2014 / 18:44
0
$connection = new mysqli("localhost", "user", "pass", "database");
$sql = $connection->query("Select * From tb_trabalhador and tb_detalhe_trabalhador");

if($sql){ // If $sql is True
    while($exibe = $sql->fetch_assoc()){
        foreach($exibe as $key => $value){
            echo "<br />" . $value;
        }

    }
}

This form exempts you from using these html tags in your code. The foreach displays all information, without specifying what should be displayed, simplifying your code, eg. $ displays ["Name"].

    
30.01.2017 / 14:50