Jump to content

Order By Date


pgrevents

Recommended Posts

Thanks for that I have tried implamenting that ill show you my code;

 

<?php
$query = mysql_query("SELECT * from programme ORDER BY expiry DESC WHERE expiry > (curdate(), INTERVAL 1 WEEK)") or die(mysql_error());
if(empty($query)){
print "NOT VALID";
}

while($row=mysql_fetch_array($query, MYSQL_ASSOC)){
?>
<?=$row['pid']?><br/>
Expiry : <?=$row['expiry']?><br/>
<hr>
<? } ?>

 

This throws back an error which is

 

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 'WHERE expiry > (curdate(), INTERVAL 1 WEEK)' at line 1

Link to comment
https://forums.phpfreaks.com/topic/162428-order-by-date/#findComment-857346
Share on other sites

Sorry, you have to reverse the order of the clauses, my fault.  Also, if you only want 1 username returned, you have to specify that with the LIMIT clause.

 

$query = mysql_query("SELECT * from programme WHERE expiry > (curdate(), INTERVAL 1 WEEK) ORDER BY expiry DESC LIMIT 1") or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/162428-order-by-date/#findComment-857347
Share on other sites

hi thanks for that but it displays the whole database but in date order is there away to show just the last seven days?

 

That's what this clause is supposed to filter out:

 

WHERE expiry >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)

 

Try to echo it out and see what's actually being passed to mysql_query.

 

$sql = "SELECT * FROM programme WHERE expiry >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) ORDER BY expiry DESC";
echo "Query => " . $sql;
$query = ($sql) or die(mysql_error());
if(empty($query)){

Link to comment
https://forums.phpfreaks.com/topic/162428-order-by-date/#findComment-858114
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.