bigrossco Posted February 8, 2007 Share Posted February 8, 2007 I have the code below but would like to modify it so it will sort the output by the date Thanks r <?php $host = "$lang_dbhost"; $user = "$lang_dbuser"; $pass = "$lang_dbpass"; $db = "$lang_dbase"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); $query = "SELECT id, DATE_FORMAT(date,'%d/%m/%Y'), time, tech, description FROM calander"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); ?> <form method="post"> <?php if (mysql_num_rows($result) > 0) { echo"<table border = 1>"; while($row = mysql_fetch_row($result)){ echo"<tr>"; echo"<td><b>Date: </b>".$row[1]." <b>Time: </b>".$row[2]." <b>Staff: </b>".$row[3]." <a href=\"./update-appoint.php?id=".$row[0]."\">$lang_update</a> <a href=\"./delete-appoint.php?id=".$row[0]."\">$lang_delete</a> </b></td>"; echo"</tr>"; echo "<td>" . $row[4]."</td>"; //echo"</table>"; } } else { echo"<b>Their is currently no appointments</b>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/ Share on other sites More sharing options...
obsidian Posted February 8, 2007 Share Posted February 8, 2007 Basically, since you're formatting the date, you're better off selecting it twice (once with formatting and once without). That way, you can ORDER BY the unformatted date: <?php $sql = mysql_query("SELECT date, DATE_FORMAT(date, '%d/%m/%Y') AS formatted, time, tech, description FROM calendar WHERE date = DATE() ORDER BY date"); ?> Notice also that if your datatype of the column is a DATE field, it's already in the %Y-%m-%d format, so you can lose the second DATE_FORMAT() call for a simple DATE() call instead. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-179923 Share on other sites More sharing options...
bigrossco Posted February 8, 2007 Author Share Posted February 8, 2007 i get the message Error in query: . Query was empty when i do your code Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-179933 Share on other sites More sharing options...
trq Posted February 8, 2007 Share Posted February 8, 2007 Date is a reserved word in sql. <?php $sql = mysql_query("SELECT `date`, DATE_FORMAT(`date`, '%d/%m/%Y') AS formatted, time, tech, description FROM calendar WHERE `date` = DATE() ORDER BY `date`"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-179944 Share on other sites More sharing options...
bigrossco Posted February 9, 2007 Author Share Posted February 9, 2007 still get the same message Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-180565 Share on other sites More sharing options...
AndyB Posted February 9, 2007 Share Posted February 9, 2007 Is your table name calander or calendar .. there's some confusion in this thread about that, and .. why not have a more useful error message? For example: $result = mysql_query($query) or die("Error ". mysql_error(). " with query ". $query); Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-180579 Share on other sites More sharing options...
tauchai83 Posted February 9, 2007 Share Posted February 9, 2007 you try to add Group By in the sql statement and see what happen. Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-180580 Share on other sites More sharing options...
bigrossco Posted February 9, 2007 Author Share Posted February 9, 2007 as in first post its calander i tried adding $result = mysql_query($query) or die("Error ". mysql_error(). " with query ". $query); the message i get is Error Query was empty with query Quote Link to comment https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-180602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.