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

0

I'll try to explain it better, like this, when listing something from the database with DESC id it shows something like this:

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

But I want it to look 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 that, the last id's will not appear if I give a LIMIT !.

For those who want to see a piece of 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;
}
    
asked by anonymous 02.08.2016 / 04:23

1 answer

1

Just like the Bacco already explained to you, you are using bind_param incorrectly. This method passes a variable to the previously defined SQL command. If you "start" your query using 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 since you are passing the value of the variable $id via bind_param , it has been replaced in the SQL command.

If your quadroAlunos method is only to lyse ALL students, the parameter id declared in the function is useless and has no use whatsoever. I would advise you to review what you are trying to implement.

In this 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();
    
02.08.2016 / 15:04