Speedysnail6 Posted September 16, 2013 Share Posted September 16, 2013 Hello. This is my PHP/MySQL code <?php $result = mysqli_query($con,"SELECT * FROM tutorials"); ?><table border="1"> <?php while($row1 = mysqli_fetch_array($result)) { echo '<tr>'; echo '<td><p>'; echo $row1['Name'] . "</p></td><td>" . $row1['ShortDescription']; echo '</p></td>'; echo '</tr>'; } ?></table> When it displays, it shows the newest columns in the table last. How do I reverse that. Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/ Share on other sites More sharing options...
Barand Posted September 16, 2013 Share Posted September 16, 2013 By using an "ORDER BY ..." clause in your query. As I don't know your table structure there is no way I can tell you any more. If you have a date column, then order by date DESC Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449624 Share on other sites More sharing options...
Speedysnail6 Posted September 16, 2013 Author Share Posted September 16, 2013 (edited) By using an "ORDER BY ..." clause in your query. As I don't know your table structure there is no way I can tell you any more. If you have a date column, then order by date DESC I do not have a date column. I could create one, but is is possible without? Edited September 16, 2013 by Speedysnail6 Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449626 Share on other sites More sharing options...
jazzman1 Posted September 16, 2013 Share Posted September 16, 2013 Show us your database structure as Barand said, with some sample of data. Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449627 Share on other sites More sharing options...
Psycho Posted September 16, 2013 Share Posted September 16, 2013 I do not have a date column. I could create one, but is is possible without? Not really. If you have an auto-increment field you can order by that field - but it is not guaranteed to be in order based upon when the records were created. It 'probably' is, but that's a poor way to order records by the date created. You'll probably want to create a date field for this. The good thing is you can create the field so it is auto-populated with the date/time of when the record is created and you don't need to put anything in your INSERT queries. But, you will need to set the value for the records that were already created. Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449628 Share on other sites More sharing options...
Speedysnail6 Posted September 16, 2013 Author Share Posted September 16, 2013 Show us your database structure as Barand said, with some sample of data. Name Owner ShortDescription Content SwagTutorial Speedysnail6 This tutorial is cool! TWUgdG9vIQ== Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449629 Share on other sites More sharing options...
Speedysnail6 Posted September 16, 2013 Author Share Posted September 16, 2013 (edited) Not really. If you have an auto-increment field you can order by that field - but it is not guaranteed to be in order based upon when the records were created. It 'probably' is, but that's a poor way to order records by the date created. You'll probably want to create a date field for this. The good thing is you can create the field so it is auto-populated with the date/time of when the record is created and you don't need to put anything in your INSERT queries. But, you will need to set the value for the records that were already created. Okay well thank you. I was wondering if it was possible without date, because it seemed to do a fine job making the newest last . Thank you. I created a data field, and I chose the default value to Current Timestamp using phpmyadmin Edited September 16, 2013 by Speedysnail6 Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449630 Share on other sites More sharing options...
jazzman1 Posted September 16, 2013 Share Posted September 16, 2013 (edited) Next time, when you've beeng asked for someone to provide a database structure, the esiest steps are:1.Open up the console and connect to your mysql db server. This is an example in Linux: [jazz@centos-box ~]$ mysql -h dbAddress -u userName -pdbPass 2. Set up your database with next SQL command: USE mydb; 3. To show us your table structure: SHOW COLUMNS FROM mytable FROM mydb; That's all! Edited September 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449633 Share on other sites More sharing options...
Psycho Posted September 16, 2013 Share Posted September 16, 2013 I was wondering if it was possible without date, because it seemed to do a fine job making the newest last . That's because the DB "typically" returns records in the order they were created. But, there is absolutely no guarantee that this will be the case. Just because it works today doesn't mean it will work that way tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449640 Share on other sites More sharing options...
vinny42 Posted September 16, 2013 Share Posted September 16, 2013 But, there is absolutely no guarantee that this will be the case. Indeed, the default order is the order in which the records appear in the datafiles and if you do something as simple as an UPDATE you will cause those files to be modified. UPDATE usually causes the database to write a new record at the end of the file, instantly making it the newest record, even if the content is quite old. If the administrator does maintenance, or a backup is restored, then things go very pear shaped. Quote Link to comment https://forums.phpfreaks.com/topic/282175-mysql-sort-by-date/#findComment-1449655 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.