V Posted July 16, 2010 Share Posted July 16, 2010 I have a query that counts the number of items that belong to a category and echoes the date of the most recent item in that category. All works fine except for the date. For some reason it isn't querying the most recent. The code is.. DB Connection ..... query all categories { ..... $sql = "SELECT *, COUNT(post_id) as total_posts FROM posts WHERE cat_id = {$row["cat_id"]} ORDER BY post_date DESC"; $posts_result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $posts_result->fetch_assoc()) { $total_posts = $row['total_posts']; $post_date = $row['post_date']; echo $total_posts; echo $post_date; }//end posts loop }//end category loop I'm not sure what I'm doing wrong. I have ORDER BY post_date DESC which I think should retrieve the most recent date.. :-\ Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/ Share on other sites More sharing options...
bh Posted July 16, 2010 Share Posted July 16, 2010 Hi, Try this: ORDER BY 'post_date' DESC" Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087200 Share on other sites More sharing options...
V Posted July 16, 2010 Author Share Posted July 16, 2010 thanks for the suggestion bh. The single quotes don't change anything. What are they indented to do? I use them in queries just for variables. Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087212 Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2010 Share Posted July 16, 2010 What is the data type of the post_date field? Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087228 Share on other sites More sharing options...
V Posted July 16, 2010 Author Share Posted July 16, 2010 Pikachu, sorry I forgot to add that. post_date is TIMESTAMP and echoes for example, 2010-07-17 12:48:03 Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087250 Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2010 Share Posted July 16, 2010 That's odd. It should work. Can you paste the query string into phpMyAdmin (if you use it) and see what happens? Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087264 Share on other sites More sharing options...
V Posted July 16, 2010 Author Share Posted July 16, 2010 Thanks for the suggestion. I tried SELECT * FROM `posts` ORDER BY post_date ASC and it lists the newest on top correctly like this 2010-07-17 13:28:12 2010-07-17 12:48:36 2010-07-17 12:48:03 Quote Link to comment https://forums.phpfreaks.com/topic/207968-order-by-date-desc-doesnt-work/#findComment-1087285 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.