2017-02-15 116 views
0

请帮助 - 查询首先打印旧消息。我需要改变顺序。mysql查询反向打印

$query = "SELECT post_title, `post_content`, `post_name` FROM wp_posts WHERE post_date > '$start' AND post_date < '$end' AND post_type='news' AND post_status = 'publish'"; 
    $result = mysql_query($query); 
    $news = ''; 

    if (mysql_numrows($result) <= 0) { 
     echo "No results for - NEWS<br/><br/>"; 
    } else { 
     while ($cnews = mysql_fetch_assoc($result)) { 


      $post_title = $cnews['post_title']; 
      $text = formatText_mt(($cnews['post_content'])); 

      $news = '<a href=“#’.$cnews['post_name'].'">' . ($post_title) . '</a><br><br>'; 
      echo $news; 
     } 
    } 

回答

1

您想使用MySql ORDER BY来订购您的结果。

"SELECT post_title, `post_content`, `post_name` FROM wp_posts WHERE post_date > '$start' 
AND post_date < '$end' AND post_type='news' 
AND post_status = 'publish' ORDER BY post_date ASC" 

您可以<sql> ORDER BY <column>ASC结束或DESC结束。

Read the manual

也确实,STOP使用MySQL_功能。您应该使用MySQLi_PDO PHP /数据库接口。 mysql_已被删除,这意味着它是不安全的,不再维护或支持。

而且mysql_numrows ==>你的意思是mysql_num_rows

你也容易受到SQL Injection,应使用预处理语句使用参数化的查询。 Read more here

+0

谢谢:) ORDER BY post_date DESC工作 – LAB

0

您需要在表示日期时间的列上使用ORDER BYDESC