How do I get records from my database in ascending order(of id), but I want it to take one by one

Asked

Viewed 67 times

-1

<?php

    $conn = mysqli_connect('localhost', 'root', '');
    if($conn){
        $Selectdb = mysqli_select_db($conn, 'quiz');
    }

    $sql = "SELECT * FROM perguntas ORDER BY id_pergunta LIMIT 1";
    $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>';


?>
  • There’s no point in you taking one by one from your database. Take the maximum amount of records you want and go through the records recovered from one to one in your PHP application. The ORDER BY clause will be useful to you.

  • I’ve tried to put "SELECT * FROM perguntas ORDER BY id_pergunta DESC" but it’s only the first question.

  • Do you mean that in your database there are several records but your PHP script only shows one? Maybe because the display is being made outside the loop?

  • then select has to be inside while? But if I leave inside Lopp mysqli_query is incomplete.

  • No, inside your loop you simply go assigning what you recovered from the bank but only exit the loop and display the last.

  • I did not understand the logistics, could be clearer and more detailed, I am new programming me.

Show 1 more comment

2 answers

0

Just after the "id_question" in the $sql statement in the fourth line, remove the "LIMIT 1" and add the ASC sorter, so that your data comes from the database in ascending order. Before:$sql = "SELECT * FROM perguntas ORDER BY id_pergunta LIMIT 1"; Afterward $sql = "SELECT * FROM perguntas ORDER BY id_pergunta ASC";. That solves one of your problems.

  • Thank you, but I don’t know how to do the second part

  • I could tell you what fields you have in your database, so I can recreate the situation and analyze it?

  • Tem os seguintes campos: id_pergunta, peprgunta, respostaa, respostab, respostac, respostad,resposta_correta, valor_questão. (exatamente nesta ordem)

0

Remove "LIMIT 1" from the SQL command Would look like this:

$sql = "SELECT * FROM perguntas ORDER BY id_pergunta";

You can, if you want, add the "ASC" in the command, which would be to query in ascending order, thus staying:

$sql = "SELECT * FROM perguntas ORDER BY id_pergunta ASC";

But I do not consider the CSO necessary, as it consults in ascending order by default.

Browser other questions tagged

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