-1

I have a while loop that loops through an array of user and admin messages. Each message holds a timestamp value. When I use the sql query:

SELECT * FROM Messages 
WHERE user_id=.... 
ORDER BY message_timestamp DESC

this of course displays the most recent messages first. But when I use the same sql query in the while loop in PHP to fetch the messages:

$select_message = ($con, "SELECT * 
                            FROM Messages 
                            WHERE user_id='....' 
                            ORDER BY message_timestamp DESC");

while($fetch_message = mysqli_fetch_array($select_message))

they are displayed but in ASC order, even when I have 'ORDER BY DESC'... I also tried ORDER BY ASC to see what will happen, but no changes...

This is what I have inside of the while loop...

  if($fetch_message['message_author'] == 'aut1') {
  echo "aut1";
  } elseif($fetch_message['message_author'] == 'aut2') {
  echo "aut2";
  }
}

phpMyAdmin Schema. Timestamp is of value type TIMESTAMP

    SELECT `message_id`,`message_author`,`user_id`,`consumer_email`,`message_content`,`admin_id`,`message_timestamp` FROM Messages;

I have been looking through some user questions on here but seems to work for most people.. could you help?

mikey1091
  • 7
  • 2
  • 1
    Maybe you should show us what is going on inside the while loop, because it is so unlikely that the Order By is failing just for you – RiggsFolly Feb 16 '21 at 09:56
  • Also if the issue is query related, showing us the schema for any related tables is good – RiggsFolly Feb 16 '21 at 09:59
  • There's not enough information in your question to help you find your bug. Since it is unknown where the bug hides, it's difficult to say what we would need to find it. Sorry. – KIKO Software Feb 16 '21 at 10:00
  • To get errors out of PHP even in a LIVE environment add these 4 lines to the top of any `MYSQLI_` based script you want to debug `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. This will force any `MYSQLI_` errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Feb 16 '21 at 10:05
  • @RiggsFolly Thank you. – mikey1091 Feb 16 '21 at 10:09

1 Answers1

0

I think you forgot to execute the query before fetching the result. If you check out the mysqli documentation, You can see that you should first execute the query and then, fetch the result.

$query = "SELECT * FROM Messages WHERE user_id='....' ORDER BY message_timestamp DESC";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
   // process the data
}

Here, i'm using MYSQLI_ASSOC fetching method, but you could also use MYSQLI_NUM or MYSQLI_BOTH

MrAnyx
  • 184
  • 2
  • 3
  • I did execute the query before the while loop, like u additionally demonstrated, but it still does not order is in DESC.. – mikey1091 Feb 16 '21 at 10:07
  • Mickey, then you should show us that. Please remember, we can only help if we can see all the relevant code, anything you miss out, looks like an error to us. People either just ignore the question, or make guesses, or Downvote the question as poorly written/asked – RiggsFolly Feb 16 '21 at 10:11