SQL DateTime Comparison - Post ID 165238

User 463868 Photo


Registered User
89 posts

Hi Everyone: I have a colum in a table that has a datetime stamp and I want to find the last posted or updated record in the table. I update the stamp after an update to the record but not sure how to compare them? I think its just x < y then... but not sure.
Any help would be appreciated.
Thanks
hogansbassets.com
eagleeye360.com
freight360.us
User 364143 Photo


Guest
5,410 posts

select * from your_table order by your_datetime_column desc limit 1;
CoffeeCup... Yeah, they are the best!
User 463868 Photo


Registered User
89 posts

Well that's easier than what I came up with...

while($row = mysql_fetch_array($result))
{
$lastupdated = $row['created_at'];
if ($row['id'] == 1)
{
$firstupdated = $row['created_at'];
}
if ($firstupdated > $lastupdated)
{
$firstupdated = $lastupdated;
}
}

Thanks Bud!!
hogansbassets.com
eagleeye360.com
freight360.us
User 463868 Photo


Registered User
89 posts

Tom: I ran the code that you gave me it said...:(
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home1/*********/public_html/Notes.php on line 57

Under the above code I put...

echo "Other method from Tom to get last updated";
echo "</br>";
$result = mysql_query("select * CustomShotRecordForm order by created_at desc limit 1");
$row = (mysql_fetch_array($result)); //<<<line 57
$last = $row['id'];
echo $last;

Why is it saying this?
hogansbassets.com
eagleeye360.com
freight360.us
User 463868 Photo


Registered User
89 posts

You can see it at hogansbassets.com/Notes.php
hogansbassets.com
eagleeye360.com
freight360.us
User 364143 Photo


Guest
5,410 posts

Check the column names carefully. Try appending this to that line to give you a better understanding of the error.

or die(mysql_error());

I'm no PHP/MySQL guru mind you. Some folks who visit here infrequently are though. If you don't have any luck this forum is pretty helpful for programming questions.

http://www.codingforums.com/

Good luck.
CoffeeCup... Yeah, they are the best!
User 463868 Photo


Registered User
89 posts

If you don't know I drive a truck and have not had time to try to work on it yet, but I tried both las night and I had to modify my cade as well...
$T = $row['created_at'];
if ($row['id'] == 1)
{
$firstupdated = $row['created_at'];
$lastupdated = $row['created_at'];
}

if ($firstupdated > $T)
{
$firstupdated = $T;
}
if ($lastupdated < $T)
{
$lastupdated = $T;
}
$last = $row['id'];

It works now :) I'll try figuring out the other next chance I get. Thanks.
hogansbassets.com
eagleeye360.com
freight360.us
User 463868 Photo


Registered User
89 posts

Check this one out
Select Max(YourDateTimeStampColume) from YourTable...LastPosted
Select Min(YourDateTimestampColume) from YourTable..FirstPosted
:D
hogansbassets.com
eagleeye360.com
freight360.us
User 364143 Photo


Guest
5,410 posts

You are on your way to be the PHP/MySQL Master! :)
CoffeeCup... Yeah, they are the best!

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.