Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!