-1

Can anyone help me out.

I'm trying to order by date DESC but it's not working.

See the code below am I missing something???

$sql = ("SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY date DESC limit 30");
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
}

In advance thanks

  • 3
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 03 '15 at 12:48
  • 6
    what is the datatype of your date field ? – kamal pal Jun 03 '15 at 12:48
  • I agree with @JayBlanchard, You should stop using mysql_* functions. And to your question, what is the result of your query? Do you get results and they are not sorted or do you get an error and can't execute the query? – Ron Dadon Jun 03 '15 at 12:50
  • This won't solve your problem, but if possible i would choose another name for this field like `creation_date` or `modified_at`. Though the word `date` is not a reserved keyword, it is also the name of a mysql function. – martinstoeckli Jun 03 '15 at 12:53
  • You know you can edit the question to give more information? It would be helpful to know about the datatype of the field, as kamal asked. Did you run the query in the db admin panel (like phpmyadmin), was the result correct? – martinstoeckli Jun 03 '15 at 13:15
  • The data type i'm using is date but even when i try to order by id desc is still not showing correctly. and yes i do get the results but not in the order i want them! – Johnny Volk Jun 03 '15 at 22:20

2 Answers2

0

If you use mysql , it is easy to use timestamp as data type , this will make your life easier as timestamp used easily in php and mysql and is easily arranged and formatted .

mercury
  • 2,390
  • 3
  • 30
  • 39
  • Using `DateTime` as data type has its advantages too, unlike (unix) `timestamps`, they do not run out in [year 2038](https://dev.mysql.com/doc/refman/5.5/en/datetime.html). Using DateTime should be as easy as using timestamp. – martinstoeckli Jun 03 '15 at 14:29
0

Don't use date as a name in the MySql DB. Because it is not accessble.

You also consider below one.

Are you use name type as date in the MySql DB?. You Should set the type as date to use DESC / ASC .

MY Simple Code. This is Work:

<?php    

$rr=mysqli_query($con, "SELECT * FROM date_sort ORDER BY my_date DESC");
while($row=mysqli_fetch_assoc($rr))
{
    $date1=$row['my_date'];
    echo "</br>".$date1."";
}

?>
Kalaivanan
  • 459
  • 2
  • 8
  • 17