Database with php

Asked

Viewed 153 times

4

I am trying to perform a database query with the following script:

<?php
        require("conexao.php");

        $exibir = mysql_query("SELECT * FROM produtos");
        while ($r = mysql_fetch_array($exibir)){
            $id = $r['id'];
            $nome = $r['nome'];
            $altura = $r['altura'];
            $peso = $r['peso'];
            $imc = $r['imc'];
        }    
?>
<?php
            echo "$nome <br/>";

?>

The code above was to return all table names, but only returns me the last !

I’d like to have all the names turned around!!

What is wrong?

  • Already started using obsolete functions and removed from new versions of PHP.

  • 1

    You’re looping the result, and in the end you’re displaying a variable, so it’s the last result because the loop rotated them all. The last thing added in $nome was the value of the last record.

  • that Samuel, use mysqli_query() and mysqli_fetch_array() as @rray quoted.

3 answers

5

The problem is that you are writing the name out of the repeat loop. The term "while" means "while" and represents a repeat structure. In this case

Enquanto houver registro no banco de dados, faça:
    Faça Alguma coisa
Fim Enquanto

Move your echo into the loop.

    require("conexao.php");

    $exibir = mysql_query("SELECT * FROM produtos");
    while ($r = mysql_fetch_array($exibir)){
        $id = $r['id'];
        $nome = $r['nome'];
        $altura = $r['altura'];
        $peso = $r['peso'];
        $imc = $r['imc'];

        // Você deve escrever aqui o nome para aparecer todos os nomes.
         echo $nome . '<br />';
    }    

3

To display all table values, leave the instruction that prints something on the screen(echo) while and not outside, otherwise only the last result will be presented.

Code with problem:

 while ($r = mysql_fetch_array($exibir)){
            $id = $r['id'];
            $nome = $r['nome'];
        }    
?>
<?php
            echo "$nome <br/>";

Sure code:

<?php
        require("conexao.php");

        $exibir = mysql_query("SELECT * FROM produtos");
        while ($r = mysql_fetch_array($exibir)){
            $id = $r['id'];
            $nome = $r['nome'];
            $altura = $r['altura'];
            $peso = $r['peso'];
            $imc = $r['imc'];
            echo "$nome <br/>"; //<---- aqui está a diferença
        }    
    ?>

Recommended reading:

Mysqli vs PDO - which is the most recommended to use?

Why should we not use mysql type functions_*?

How to prevent SQL code injection into my PHP code

0

Note that you created while correctly, but as While is going through all the records of your select, your line echo "$nome <br/>"; must also come within while for as long as it finds a name it displays this name and so will it with all names found

 <?php
    require("conexao.php");

    $exibir = mysql_query("SELECT * FROM produtos");

    while ($r = mysql_fetch_array($exibir)){
        $id = $r['id'];
        $nome = $r['nome'];
        $altura = $r['altura'];
        $peso = $r['peso'];
        $imc = $r['imc'];

        echo "$nome <br/>";
    }               
?>

Browser other questions tagged

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