如何使值最近下降?

时间:2015-04-08 06:57:34

标签: php mysql

我正在制作一个显示人名及其评论的基本评论程序。所以我想知道如何通过最近评论而不是第一,第二,第三,第四,第五来显示评论。

的index.php

<?php //index.php

    require_once 'login.php';
    $db_server = mysql_connect($db_hostname, $db_username, $db_password);
    if(!$db_server) die("Unable to connect to server: " . mysql_error());
    mysql_select_db($db_database, $db_server) or die("Unable to select database:       ");

    if (isset($_POST['post_name']) && isset($_POST['post_message'])) {
    # code...
    $post_name = get_post('post_name');
    $post_message = get_post('post_message');

    $query = "INSERT INTO posts VALUES" . "('','$post_name', '$post_message')";
    if(!mysql_query($query, $db_server)){
        echo "INSERT FAILED: $query <br />";
        mysql_error() . "<br /><br />";
    }
    }

    echo <<<_END
    <form action="index.php" method="post">
    <pre>
    Name    <input type="text" name="post_name" required/>
    Message <textarea rows="4" cols="50" name="post_message" required>Enter ur                message here lol</textarea>
    <input type="submit" value="submit" />

    </pre>
    </form>
    _END;

    $query = "SELECT * FROM posts";
    $result = mysql_query($query);
    if(!$result) die("database acess failed:" . mysql_error());
    $rows= mysql_num_rows($result);
     for ($i=0; $i < $rows; $i++) { 
    # code...
    $row = mysql_fetch_row($result);
    echo <<<_END
    <pre>
    Name: $row[1]
    Message: $row[2]

    </pre>

    _END;
    }

    function get_post($var){
    return mysql_real_escape_string($_POST[$var]);
    }

    ?>

此表的Sql文件

CREATE TABLE IF NOT EXISTS `posts` (
post_id int(10) unsigned NOT NULL AUTO_INCREMENT,
post_name varchar(100) NOT NULL,
post_message varchar(150) NOT NULL,
PRIMARY KEY (`post_id`)
)

1 个答案:

答案 0 :(得分:0)

添加&#34; ORDER BY post_id DESC&#34;到查询字符串。

$query = "SELECT * FROM posts ORDER BY post_id DESC";