在MySql表中显示最新的数据

时间:2014-12-03 19:52:33

标签: php html mysql

尝试在html表中显示mysql表数据。我需要在最上面的帖子中显示最新的帖子。

目前,当我以这种方式制定代码时,它会向我显示从表格底部的表单中发布的最新数据。

请帮帮我。感谢。



<?php
echo "<table style='border: solid 3px black;'>";
echo "<tr><th>Name</th><th>Level</th><th>Region</th><th>Event</th><th>Comments</th><th>Posting Date</th></tr>";

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:150px;border:1px solid black'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}

$servername = "localhost";
$username = "Frank07";
$password = "xxxxx";
$dbname = "table_d";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM dnew2");
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
    $dsn = null;
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
echo "</table>";
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你可以这样做:

SELECT * FROM dnew2 ORDER BY post_date DESC

您可以使用ASC或DESC。