我在我的数据库上运行了查询并返回了正确的降序,但是,在我的网站上,图像仍然按升序显示。我希望ID更高的图像显示在页面顶部。
<?php
$sql = "SELECT * FROM image ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='imageContainer'>"
."<h1>".$row["name"].'</h1>'
.'<a href="imageInfo.php?image='.$row["path"].'"><img class="uploadedImg" src="uploads/'.$row["path"] .'" alt="Random image" /></a>
</div>';
}
?>
答案 0 :(得分:1)
这是解决方案。它在我的系统中工作。请看看,让我知道。如果有任何更多的错误。 在这个我使用的数据库连接对象。 和方法我使用$ result-&gt; fetch_assoc()。以下是有关此方法的详细信息的链接:http://php.net/manual/en/mysqli-result.fetch-assoc.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM image ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// echo "<div class='imageContainer'><h1>".$row["name"]."'</h1><a href="imageInfo.php?image='.$row["path"].'"><img class="uploadedImg" src="uploads/'.$row["path"] .'" alt="Random image" /></a></div>";
echo "<div class='imageContainer'>"
."<h1>".$row["name"].'</h1>'
.'<a href="imageInfo.php?image='.$row["path"].'"><img class="uploadedImg" src="uploads/'.$row["path"] .'" alt="Random image" /></a>
</div>';
}
} else {
echo "0 results";
}
$conn->close();
?>