Jump to content

MySQL sort by date


Speedysnail6

Recommended Posts

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.

Link to comment
Share on other sites

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 by Speedysnail6
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :happy-04:.

Thank you. I created a data field, and I chose the default value to Current Timestamp using phpmyadmin

Edited by Speedysnail6
Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

I was wondering if it was possible without date, because it seemed to do a fine job making the newest last :happy-04:.

 

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.

Link to comment
Share on other sites

 


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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.