How to display all the results of a SELECT in PHP?

3

I've done this code all by myself, but I'm having trouble displaying all the results one below the other.

    <?php include 'conexao.php'; ?>
<html>
    <head>
        <title>GMB Mineração e Comércio LTDA</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" href="css/default.css"/>
    </head>
    <body>    
        <h1>Clientes</h1>
        <fieldset class="row2">
          <legend><font color="white">-</font></legend>
            <p>
                <?php
                    $query = "SELECT * from clientes";
                    $result = mysql_query($query);
                    $fetch = mysql_fetch_row($result);
                    $final = $fetch[0];
                ?>
            </p>
        </fieldset>
    </body>
</html>
    
asked by anonymous 02.01.2015 / 19:41

3 answers

5

So:

<?php
    $query = "SELECT * from clientes";
    $result = mysql_query($query);
    while($fetch = mysql_fetch_row($result)){
        echo "<p>". $fetch[0] . " - " . $fetch[1] . " - " . "</p>";
    }
?>

There are a number of other ways to do this. It depends on the specific need.

You could do something like this (more recommended (but just an example, I do not know how your structure is):

<?php
    $query = "SELECT cnpj, nome from clientes";
    $result = mysql_query($query);
    while($fetch = mysql_fetch_row($result)){
        echo "<p>". $fetch[0] . " - " . $fetch[1] . " - " . "</p>";
    }
?>

In this way you only have the fields that you will use. But if you put all the fields of the same table, you can do this:

<?php
    $query = "SELECT * from clientes";
    $result = mysql_query($query);
    while($fetch = mysql_fetch_row($result)){
        echo "<p>"
        foreach ($fetch as $value)
            echo $value . " - ";
        }
        echo "</p>";
    }
?>

You can still do it better. But I think the idea is just to give you the basic idea, ideally you should mount it using a <table> or a more structured way to organize this data. But I think you should take it one step at a time.

Ideally, you should also use the MySQLi which is a more modern and recommended API.

    
02.01.2015 / 19:46
2

Add a while in mysql_fetch_* this way PHP evaluates whether the function returns true or false, if positive assignment is made. To display the 40 columns 'automatically' create a for.

$str = '';                      
while($linha = mysql_fetch_row($result)){
   for($i=0; $i<40; $i++){
      $str .= $linha[$i] .' - ';
   }
}
echo $str . '<br>'; 

The mysql_ * functions are already out of date for some time, see why: Why we should not use functions of type mysql_ *? , the recommended is to use mysqli or PDO.

    
02.01.2015 / 19:44
2

The correct thing is to use MySQL, not MySQL.

<?php
$MySQLi = new MySQLi( 'servidor', 'usuario', 'senha', 'banco' );
$query = "SELECT * from clientes";
$result = $MySQLi->query($query);
while($fetch = $result->fetch_assoc()) {
    echo "<p>";
    foreach ($fetch as $field => $value) {
        echo $field . ' => ' . $value . ' | ';
    }
    echo "</p>";
}
?>

mysql_ * functions have been deprecated since PHP 5.5. It is preferable to use MySQLi, as I have shown above or PDO, which is even more advantageous. See more here: link

    
08.01.2015 / 20:16