Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
ORDER BY is not working with the session

I want to get the latest record from the table session_details therefore I used ORDER BY ... DESC but the output isn't coming. i have user a session inorder to get the value from the login itself.

    <head>
    <title></title>
</head>
<body>
<a href="../includes/logout.php"> logout mofo </a><br>

<?php
if(!isset($_SESSION)) 
    { 
        session_start(); 
    }

include_once('../includes/connection.php');

echo $_SESSION['id'];

$sql = "SELECT * FROM session_details  WHERE student_id = '" . $_SESSION['id'] . "' ORDER BY 'session_id DESC LIMIT 1 ";
$result = mysqli_query($conn, $sql);

if ($result) {
    while($row = mysqli_fetch_assoc($result)) {
    echo "Session ID:" . $row["session_id"] . "<br>";
    echo "ID: " . $row["student_id"] ."<br>";
    echo "Student Name: " . $row["student_name"]. "<br>";
    echo "Lecturer Name: " . $row["supervisor_name"]. "<br>";

    }
} else {
    echo "0 results";
}

?>

</body>
</html>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

session_start() should be done first before anything is sent t the output buffer. It does not need to be inside a test, just start the session in every script that needs it before doing anything else.

<?php
    session_start(); 
?>
<head>
    <title></title>
</head>
<body>
<a href="../includes/logout.php"> logout mofo </a><br>

<?php
include_once('../includes/connection.php');

echo $_SESSION['id'];

$sql = "SELECT * 
        FROM session_details  
        WHERE student_id = '" . $_SESSION['id'] . "' 
        ORDER BY session_id DESC LIMIT 1 ";
$result = mysqli_query($conn, $sql);

if ($result) {
    while($row = mysqli_fetch_assoc($result)) {
    echo "Session ID:" . $row["session_id"] . "<br>";
    echo "ID: " . $row["student_id"] ."<br>";
    echo "Student Name: " . $row["student_name"]. "<br>";
    echo "Lecturer Name: " . $row["supervisor_name"]. "<br>";

    }
} else {
    echo "0 results";
}
?>
</body>
</html>

You should really be using parameterized bound queries to protect you against SQl Injection as well

<?php
    session_start(); 
?>
<head>
    <title></title>
</head>
<body>
<a href="../includes/logout.php"> logout mofo </a><br>

<?php
include_once('../includes/connection.php');

echo $_SESSION['id'];

$sql = "SELECT * 
        FROM session_details  
        WHERE student_id = ?
        ORDER BY `session_id` DESC LIMIT 1 ";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $_SESSION['id']);
$result = $stmt->execute();

while($row = $result->fetch_assoc()) {
    echo "Session ID:" . $row["session_id"] . "<br>";
    echo "ID: " . $row["student_id"] ."<br>";
    echo "Student Name: " . $row["student_name"]. "<br>";
    echo "Lecturer Name: " . $row["supervisor_name"]. "<br>";

}
?>
</body>
</html>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda