Reputation: 29
I'm making a simple webpage where anyone can type in a text in a textfiel, and that text will be stored to a database and placed on the screen. At the moment, the text that was posted most recently gets placed at the bottom at the page. I want it to be reversed; the newest at the top and the oldest at the bottom. Here is my code:
<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();
?>
Upvotes: 1
Views: 68
Reputation: 4858
Just update your SQL
$sql = "SELECT String FROM janne ORDER BY id DESC";
Here id
may be the Auto Increment
field in your table.
Upvotes: 2