使PHP循环以相反的顺序回显

使PHP循环以相反的顺序回显

我正在制作一个简单的网页,任何人都可以在文本框中输入文本,文本将被存储到数据库中并放在屏幕上。目前,最近发布的文本被放在页面的底部。我想把它倒过来,最新的在上面,最老的在下面。
这是我的代码:

<html>
<head><title>JANNEchat Beta</title></head>
<form action="index.php" method="post" />
<p>Send a message to JANNES database: <input type="text" name="input1" />
      </p><input type="submit" value"Submit" />
</form>

</html>
<?php

error_reporting(E_ALL ^ E_DEPRECATED);
define('DB_NAME', 'janne');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
die('Nu är något vajsing, kunde inte ansluta ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
die('Nu spelas det trix, kan inte hitta databasen ' . DB_NAME . ' : ' .
             mysql_error());   }

$value = isset($_POST['input1']) ? $_POST['input1'] : '';

if($value != ''){
$sql = "INSERT INTO janne (String) VALUES ('$value')";
if(!mysql_query($sql)){
die('vajsing: ' . mysql_error());
}
}

mysql_close();

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "janne";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT String FROM janne";
$result = $conn->query($sql);
//$a = $result->num_rows - $result->fetch_assoc();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//THE PROBLEM IS HERE, I GUESS.
    echo $row["String"] . "<br>";
   }
 } else {
 echo "0 results";
}

$conn->close();
?>

最佳答案

只需更新您的SQL

$sql = "SELECT String FROM janne ORDER BY id DESC";

这里id可能是表中的Auto Increment字段。

关于php - 使PHP循环以相反的顺序回显,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33346557/

10-13 04:08