LAB
LAB

Reputation: 1

mysql query reverse printing

Please HELP -- query is printing old news first. I need to reverse the order.

$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;
        }
    }

Upvotes: 0

Views: 29

Answers (2)

Abhishek Jain
Abhishek Jain

Reputation: 3702

You need to use ORDER BY with DESC on a column which denotes date-time.

Upvotes: 0

Martin
Martin

Reputation: 22760

You want to use MySql ORDER BY to order your results.

"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"

You can <sql> ORDER BY <column> ASCending or DESCending.

Read the manual.

Also really, STOP using MySQL_ functions. You should be using MySQLi_ or PDO PHP/Database intefaces. mysql_ is DEPRECATED meaning it is insecure and no longer maintained or supported.

And mysql_numrows ==> You mean mysql_num_rows

You are also vulnerable to SQL Injection and should be using Parameterised queries using Prepared Statements. Read more here

Upvotes: 1

Related Questions