我正在使用数据库进行聊天。当我显示消息时,旧消息位于顶部,新消息位于底部。我怎样才能做到这一点,以便新消息排在首位。我已经尝试过,ORDER BY Username DESC
但这只是让它按字母顺序排列。
这是我的完整代码
<html>
<head>
<title>Jetup</title>
</head>
<body>
<form name="chatform" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="chatuser"/>
<input type="text" name="chatinput"/>
<input type="image" name="send" value="Verzenden"/>
<br/>
<?php
$connection = mysql_connect("localhost","root","usbw") or die ("Kon geen verbinding maken");
mysql_select_db("chat") or die("Kan geen database selecteren");
if(isset($_POST['chatinput']) && isset($_POST['chatuser'])){
$input = $_POST['chatinput'];
$user = $_POST['chatuser'];
$query = "INSERT INTO chatterdata (Username, Message) VALUES ('".$user."', '" .$input."')";
mysql_query($query) or die(mysql_error());
}
$display = mysql_query("SELECT * FROM chatterdata ORDER BY Username DESC, Message DESC");
while($row = mysql_fetch_array($display)){
echo($row['Username'] . " : " .$row['Message'] . "<br/>");
}
?>
</form>
</body>