Forum Moderators: open
$q = "SELECT *, date_format(eventdate, '%m/%e/%Y') as eventdate FROM ".TBL_EVENTS." WHERE active = 'TRUE' ORDER BY eventdate ASC";
The results for ORDER BY eventdate ASC
2008-06-21 00:00:00
2008-10-04 00:00:00
2007-11-14 00:00:00
The results for ORDER BY eventdate DESC
2007-11-14 00:00:00
2008-10-04 00:00:00
2008-06-21 00:00:00
What I want:
2008-10-04 00:00:00
2008-06-21 00:00:00
2007-11-14 00:00:00
What am I doing wrong?
It is definitely the date_format in the select statement that is causing the problem. When I take it out, it orders fine. But when I put it in, the year seems to be ignored in the "ORDER BY" part. How weird that it affects the "ORDER BY" part.
SELECT *, date_format(eventdate, '%m/%e/%Y') as eventdate1
FROM ".TBL_EVENTS."
WHERE active = 'TRUE'
ORDER BY TBL_EVENTS.eventdate
This *may* force the ordering by using the actual data in eventdate instead of the aliased eventdate. I haven't tied it to know for sure, but it seems like it should work...
Here is my query string:
$result = mysql_query( "SELECT * FROM schedule WHERE status != 'Completed' && status != 'Cancelled' ORDER BY 'when' ASC" )
The output is in the order inputted into the database instead of by datetime field.
All help appreciated
added the following:
Actually, it looks like the single quotes around when is your problem. Try the field name without the single quotes.
[edited by: LifeinAsia at 4:29 pm (utc) on Sep. 5, 2008]
$result = mysql_query
("
SELECT *
FROM schedule
WHERE (status != 'Completed' && status != 'Cancelled')
ORDER BY 'when' ASC
")
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
echo "<tr><td><b>$num_rows visits listed</b></td></tr>";
while($row = mysql_fetch_array( $result )) {
$time=strtotime($row[when]);
echo "<tr><td valign='top'><b>";
echo date('l, M jS Y \a\t g:i A',$time);
echo "</b></td></tr>";
}
echo "</table>";
include("includes/dbclose.php");
?>
And I get this output:
9 visits listed
Saturday, Dec 20th 2008 at 7:00 PM
Tuesday, Dec 9th 2008 at 12:00 PM
Saturday, Dec 6th 2008 at 9:30 AM
Thursday, Dec 18th 2008 at 10:00 AM
Thursday, Dec 18th 2008 at 2:00 PM
Saturday, Nov 29th 2008 at 3:00 PM
Saturday, Dec 20th 2008 at 5:00 PM
Friday, Dec 12th 2008 at 2:00 PM
Saturday, Nov 15th 2008 at 9:00 AM
Then I removed the single quote around 'when' and I get this result:
"SELECT Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'when ASC' at line 1"
2008-12-20 19:00:00
2008-12-09 12:00:00
2008-12-06 09:30:00
2008-12-18 10:00:00
2008-12-18 14:00:00
2008-11-29 15:00:00
2008-12-20 17:00:00
2008-12-12 14:00:00
2008-11-15 09:00:00
Here's the modified script:
<table border=1 align="center">
<?PHP
include("includes/dbconnect.php");
$result = mysql_query
("
SELECT *
FROM schedule
WHERE (status != 'Completed' && status != 'Cancelled')
ORDER BY 'when' ASC
")
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
echo "<tr><td><b>$num_rows visits listed</b></td></tr>";
echo "<tr><th>datetime field</th><th>desired output</th></tr>";
while($row = mysql_fetch_array( $result )) {
$time=strtotime($row[when]);
echo "<tr><td>".$row['when']."</td><td valign='top'><b>";
echo date('l, M jS Y \a\t g:i A',$time);
echo "</b></td></tr>";
}
echo "</table>";
include("includes/dbclose.php");
?>
And this is the output:
9 visits listed
datetime field desired output
2008-12-20 19:00:00Saturday, Dec 20th 2008 at 7:00 PM
2008-12-09 12:00:00Tuesday, Dec 9th 2008 at 12:00 PM
2008-12-06 09:30:00Saturday, Dec 6th 2008 at 9:30 AM
2008-12-18 10:00:00Thursday, Dec 18th 2008 at 10:00 AM
2008-12-18 14:00:00Thursday, Dec 18th 2008 at 2:00 PM
2008-11-29 15:00:00Saturday, Nov 29th 2008 at 3:00 PM
2008-12-20 17:00:00Saturday, Dec 20th 2008 at 5:00 PM
2008-12-12 14:00:00Friday, Dec 12th 2008 at 2:00 PM
2008-11-15 09:00:00Saturday, Nov 15th 2008 at 9:00 AM
(the following is actually a table with two data cells per line:)
datetime field desired output
2008-12-20 19:00:00Saturday, Dec 20th 2008 at 7:00 PM
2008-12-09 12:00:00Tuesday, Dec 9th 2008 at 12:00 PM
2008-12-06 09:30:00Saturday, Dec 6th 2008 at 9:30 AM
2008-12-18 10:00:00Thursday, Dec 18th 2008 at 10:00 AM
2008-12-18 14:00:00Thursday, Dec 18th 2008 at 2:00 PM
2008-11-29 15:00:00Saturday, Nov 29th 2008 at 3:00 PM
2008-12-20 17:00:00Saturday, Dec 20th 2008 at 5:00 PM
2008-12-12 14:00:00Friday, Dec 12th 2008 at 2:00 PM
2008-11-15 09:00:00Saturday, Nov 15th 2008 at 9:00 AM
Here is the output using the above line in the script (eliminating "strtotime"):
9 visits listed
datetime field desired output
2008-12-20 19:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-09 12:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-06 09:30:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-18 10:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-18 14:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-11-29 15:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-20 17:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-12-12 14:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
2008-11-15 09:00:00 Wednesday, Dec 31st 1969 at 4:33 PM
9 visits listed
2008-12-20 19:00:00 1229828400
2008-12-09 12:00:00 1228852800
2008-12-06 09:30:00 1228584600
2008-12-18 10:00:00 1229623200
2008-12-18 14:00:00 1229637600
2008-11-29 15:00:00 1227999600
2008-12-20 17:00:00 1229821200
2008-12-12 14:00:00 1229119200
2008-11-15 09:00:00 1226768400
$result = mysql_query( "SELECT * FROM schedule WHERE status != 'Completed' && status != 'Cancelled' ORDER BY [when] ASC" )
the IMPORTANT part being [when] rather than 'when'
Hope that helps :-)
EDIT: Sorry probably should have explained why!
when is a reserved word in SQL so that is why you receive an error with "ORDER BY when ASC" as this makes no sense to SQL
if you have "ORDER BY 'when' ASC", SQL is looking for a reference to the object 'when' which actually doesn't exist - as 'when' has not been assigned to anything.
by using [] brackets around reserved words SQL knows that you wish to use this as an object rather than as a reserved word
Best practice is to name you fields, tables etc with prefixes eg(tbl_schedule) or (fld_when) OR use names that are not reserved words in SQL
Cheers
[edited by: Alt_F4 at 4:52 am (utc) on Oct. 16, 2008]