How do I print data by data in sql?(selects all data, but only prints one by one in array format)!

Asked

Viewed 39 times

-1




    $sql = "SELECT * FROM perguntas ORDER BY id_pergunta";
    $execsql = mysqli_query($conn, $sql) or die("Erro");
    while($row = mysqli_fetch_array($execsql)){
        # code...
        $IdPergunta = $row['id_pergunta'];
        $p = $row['pergunta'];
        $ra = $row['respostaa'];
        $rb = $row['respostab'];
        $rc = $row['respostac'];
        $rd = $row['respostad'];
        $vp = $row['valor_pergunta'];


    }
    echo '<?xml version="1.0" encoding="UTF-8" ?>';
    echo '<root>';
    echo '<Pergunta id="IdPergunta">' . $IdPergunta . '</Pergunta>';
    echo '<Pergunta id="ValorPergunta">' . $vp . '</Pergunta>';
    echo '<Pergunta id="Pergunta">' . $p . '</Pergunta>';
    echo '<Pergunta id="RespostaA">' . $ra . '</Pergunta>';
    echo '<Pergunta id="RespostaB">' . $rb . '</Pergunta>';
    echo '<Pergunta id="RespostaC">' . $rc . '</Pergunta>';
    echo '<Pergunta id="RespostaD">' . $rd . '</Pergunta>';
    echo '</root>';


?>```
  • 1

    Only the last dice passed by will appear while in your case. In order for you to get all the data, you will have to put this whole block of echo inside your while, otherwise it will not print all the data.

  • Continue on 1° question

  • 1

    I don’t understand very well, but you mean repeat the echo ?

  • 1

    I have to vote to close because I can’t understand the question. You have to be clearer, if you have difficulties in formulating questions comment on the code what to do.

  • @Joãovictor, if only one die is appearing when you put the echo inside the while, it is probably because it is returning only one data even, otherwise it will make a looping and will print all the results...

1 answer

0


Do it:

$sql = "SELECT * FROM perguntas ORDER BY id_pergunta";
    $execsql = mysqli_query($conn, $sql) or die("Erro");
    while($row = mysqli_fetch_array($execsql)){
        # code...
        $IdPergunta = $row['id_pergunta'];
        $p = $row['pergunta'];
        $ra = $row['respostaa'];
        $rb = $row['respostab'];
        $rc = $row['respostac'];
        $rd = $row['respostad'];
        $vp = $row['valor_pergunta'];

    echo '<?xml version="1.0" encoding="UTF-8" ?>';
    echo '<root>';
    echo '<Pergunta id="IdPergunta">' . $IdPergunta . '</Pergunta>';
    echo '<Pergunta id="ValorPergunta">' . $vp . '</Pergunta>';
    echo '<Pergunta id="Pergunta">' . $p . '</Pergunta>';
    echo '<Pergunta id="RespostaA">' . $ra . '</Pergunta>';
    echo '<Pergunta id="RespostaB">' . $rb . '</Pergunta>';
    echo '<Pergunta id="RespostaC">' . $rc . '</Pergunta>';
    echo '<Pergunta id="RespostaD">' . $rd . '</Pergunta>';
    echo '</root>';

    }      

?>

Browser other questions tagged

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