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

Asked

Viewed 50,069 times

3

I’ve done all this code myself, but I’m having a hard time displaying all the results below each 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>
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

5

Thus:

<?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 that (more recommended (but it’s just an example, I don’t 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>";
    }
?>

This way you bring only the fields that you will use. But if you are going to put all the fields in 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>";
    }
?>

I put in the Github for future reference.

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

The ideal would also be you use the Mysqli which is a more modern and recommended API.

  • Keeps showing errors, the answer of rray worked but is only displaying the ID of the first and second results.

  • Ah, yes, now I understand, but do I really have to write each result? For example, this table has about 40 columns :S

  • I edited it to make it easier to fit into what you need. I don’t get better because I don’t know exactly the result you want to get or the structure of your table. Note that I am showing you to do something very basic. The way you are doing is not ideal, but it works. You take what you want. You don’t have to put what you don’t want to appear. Also, if you want everything to appear, you can put another loop, internal to automate, I will see if better now that you are passing new information.

  • 1

    But always try to put as much information as possible of what you want in the question itself, even if you have to edit it.

3

Correct is to use Mysqli, 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 are obsolete since PHP 5.5. It is preferable to use Mysqli, as shown above or PDO, which is even more advantageous. See more here: http://www.ultimatephp.com.br/php-por-que-nao-utilizar-funcoes-mysql/

2

Add a while on mysql_fetch_* This way PHP evaluates whether the function returns true or false, if positive is assigned. To view 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 long overdue, see why in: Why should we not use mysql type functions_*?, the recommended is to use mysqli or PDO.

  • I delete this line? $final = $fetch[0];

  • If you are only displaying the values you can directly call no need for a variable, if you are going to save all the items for later use, create an array.

  • To view, all fields, just change the array key: echo $linha[0] .' - '. $linha[1] .' - '. $linha[2];. In your bank there are more than two lines?

  • Got it. In case you are displaying the ID of the first and second result. How do I display all data instead of one to one?

  • There are about 40 columns in this table :S

Browser other questions tagged

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