How to list mysqli data displaying in DESC but listing from top to bottom?

Asked

Viewed 151 times

0

I’ll try to explain it better, like this, by listing something from the bank with DESC id shows something like this:

id:1
id:2
id:3
id:4
id:5

but I want it to be displayed like this:

id:5
id:4
id:3
id:2
id:1

In this case using ORDER BY id ASC will not help me because I need to list several data and with this, the last id’s will not appear if I give a LIMIT!.

For anyone who wants to see a piece of the code:

public function quadroAlunos($id){
    $this->Query = $this->Conexao->prepare("SELECT * FROM quadroAlun ORDER BY ? DESC");
    $this->Query->bind_param("s",$id);
    $this->Query->execute();
    $this->ResultadoUser = $this->Query->get_result();
    while($this->Listar = $this->ResultadoUser->fetch_assoc()):

    echo "<strong class=aluno>".$this->Listar["nick"].":</strong>"; //Recupera nick
    echo "<span class=mensagem>".$this->Listar["mensagem"]."</span>"; //Recupera Mensagem
    echo "<span class=hora>".$this->Listar["data"]."</span>"; //Recupera hora da postagem
    echo "<br /><br />"; //Quebra de linhas

    endwhile;
}
  • It would be good to [Dit] the question and put the relevant part of the code to analyze. Explain the result you want to get, which is easier. And with DESC the result can’t be what you said, there’s something wrong there.

  • All right, I edited!

  • 1

    There is a problem in your code, you are passing a value where it should be a column name. With this ? there will never work ORDER. You are ordering with a fixed value. (the ID passed will always be the same)

  • Like this, the data is being listed from the bottom up, the bottom is the last id’s and the top is the first id’s, this listing like a chat, the chat you know that lists the last messages from the bottom up, being the ones below the new messages and the ones above the old ones understood ? I want to reverse this

  • 2

    They are down because this order is completely meaningless. You have to put the name of the column in the order, and not pass a value to it. This Binding there does not work as you imagine. Put ORDER BY id DESC that works (and get this Binding out of there, which is not helping you at all).

  • it really worked, vlw, there was a time I needed you to list this listing just now and I couldn’t kkkk

  • 1

    If you need to change the column you are sorting, you need to do it with strings "SELECT * FROM quadroAlun ORDER BY $coluna" - but you have to be careful not to allow SQL injection into $column. There could call quadroAlunos('nome'), for example, if you want by name, and quadroAlunos('id DESC') by id and reversed. But before complicating, finish working by ID right, not to make confusion.

Show 2 more comments

1 answer

1

Just like the Bacchus already explained to you, you are using the bind_param incorrectly. This method passes a variable to the command SQL previously defined. If you "print" your query with method debugDumpParams, you will see that your query is being mounted incorrectly, example:

SELECT * FROM quadroAlun ORDER BY 1 DESC

Note that it should be ORDER BY id DESC, but how you are passing the value of the variable $id via bind_param, she was replaced in command SQL.

If that’s your method quadroAlunos is just to smooth ALL students, the parameter id declared in the function is useless and has no use. I advise you to review what you are trying to implement.

That way, your code will look something like:

$this->Query = $this->Conexao->prepare("SELECT * FROM quadroAlun ORDER BY id DESC");
$this->Query->execute();
$this->ResultadoUser = $this->Query->get_result();

Browser other questions tagged

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