Solved

Sorting by date with php/mysql broke?

Posted on 2004-04-02
1
529 Views
Last Modified: 2012-08-14
I'm currently trying to display a mysql db and order the results by date...  With my current code, the output is put in date order to about 90%, but some of the dates are out of order for some reason.  I've been trying to figure this out for a few days now and I need help! =)  Below is my php source and a link to my php page...

<?php
$db = mysql_connect("localhost","xxxx","xxxxxxxxxxxx") or die("Problem connecting");
mysql_select_db("mikegood_test") or die("Problem selecting database");

// query one
$query = "SELECT date_format(date,'%M %D, %Y') as date, col2, col3 FROM test ORDER BY date ASC";

// query the database for lans in the next two weeks - broke
$query2 = "SELECT date_add('2004-04-01', INTERVAL 31 DAY) as date, col2, col3 FROM test ORDER BY date ASC";

// datesub()
$query3 = "SELECT DATE_SUB('2004-04-15', INTERVAL 31 DAY);
        -> '2004-03-30'";

$result = mysql_query($query) or die ("Query failed");
//get the number of rows in our result to use in the for loop
$numofrows = mysql_num_rows($result);
?>

<?php
echo "<table border=\"1\">\n";
echo "<tr bgcolor=\"lightblue\"><td>date</td><td>col2</td><td>col3</td></tr>\n";
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from the result set
    if($i % 2) { // if there is a remainder
        echo "<tr bgcolor=\"yellow\">\n";
    } else { //if there isn't a remainder
        echo "<tr bgcolor=\"white\">\n";
    }
    echo "<td>".$row['date']."</td><td>".$row['col2']."</td><td>".$row['col3']."</td>\n";
    echo "</tr>\n";
}
//close the table
echo "</table>\n";
?>

<br><br>

<?
$date = date("D j M Y, G:ia");
//$date = date("Y
echo $date;
?>

this page can be viewed live from: http://mikegood.net/displaytest.php
0
Comment
Question by:mgood16
1 Comment
 
LVL 17

Accepted Solution

by:
leannonn earned 125 total points
Comment Utility
> SELECT date_format(date,'%M %D, %Y') as date, col2, col3 FROM test ORDER BY date ASC

The sorting does not work as expected because you sort the results on the modified/formated field `date`. You might want to try this one:
---
SELECT date_format(date,'%M %D, %Y') as my_date, col2, col3 FROM test ORDER BY date ASC
---

This way you sort it by the original `date` field, not the formatted one.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WordPress AWS: site goes down 2 78
Problem with SqlConnection 5 99
MySQL Error 3 34
Instering to MySQL table 5 13
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now