I am using a mySQL query in PHP to pull content from rows into an array. A simple for statement is then displaying this data:

    for ($i = 0; $i < ($num_rows); $i++):

        //Find Author Name
        $query = 'SELECT firstname, lastname FROM vd_users WHERE id = ?';
        $statement = $mySQL_con->prepare($query);

        //Attempt Query
        try {
            $statement->execute(array($results[0][$i]['author_id']));
        }

        //Catch Errors
        catch (PDOException $error) {
            $info = $error->getMessage();
            echo 'Sorry, we failed to load this page'; if ($_SESSION['debug'] = 1) { echo ': ' . $info; }       
        }

        //Fetch Results to Array
        $row = $statement->fetch(PDO::FETCH_ASSOC);

    endfor;

This works well. However, since new rows are added with an id++, the newer ones are displayed at the bottom, and I would like them at the top.

Is it possible to order the mySQL rows by date using a column, or will I have to look at some other way? On a hunch I tried inverting the for statement to for ($i = $row_count; $i = 0; $i--) thinking that that would do it, but nope!

I tried adding ORDER BY date to my query, but that didn't work. Note that 'date' is a column name in the database.

Actually.. Ignore this. I was adding the ORDER BY to the wrong $query!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.