[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Sorting by date with php/mysql broke?

Posted on 2004-04-02
1
Medium Priority
?
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
Aleksandar Bradarić earned 375 total points
ID: 10747197
> 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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