Jump to content

[SOLVED] How to grab lastest day from db


bsamson

Recommended Posts

I have a DB and I am trying to find a way in php/mysql to grab the field w/ the most current date ...

 

Fields:

idno

date

info

 

Values:

2

04/25/07

info goes here

 

3

04/26/07

info goes here

 

In this case I would like to grab the ID # and info from IDNO 3. How would I do this? Thanks!

 

Link to comment
Share on other sites

Perhaps I am a bit confused. I need to find the date closest to todays. How do I select the closest date and also specify the ID# I want ?? Thanks for any help!

 

If the date in id 3 is 5 days ago, and the date in id 2 is 4 days ago, then the database will return the date 4 days ago in revraz's script. Also, your DATE column needs to be a DATETIME column or a TIMESTAMP for the ORDER BY to work on the query.

Link to comment
Share on other sites

What if there are 3 that are the same day, do you also store the time?

 

i don't know what you mean, or if your talking to me lol.

 

I think the seconds are never the same in a TIMESTAMP as it constantly changes WHEN the script is running, or the query sorts by the ID if there are 3 of the same dates...

Link to comment
Share on other sites

Thanks for the reply, but I was asking how he stores it not how it works.  I am familiar with how timestamps work, but if he just stores mm/dd/yy then it doesn't do any good right?

 

Trying to understand his setup so we can suggest a better way and give him the results he needs.

Link to comment
Share on other sites

yes I am sorry ... I need to select the newest for a particiular ID #.

 

For example if I want ID #5 ... there are muliples entries for the ID ... So I need to find the newest or latest entry ...

 

<?php 

$query = mysql_query("SELECT * FROM `table `ORDER BY `id` DESC LIMIT 1");

// OR

$query  = mysql_query("SELECT * FROM `table` ORDER BY `date`DESC LIMIT 1");

?>

 

 

That does the trick, the first one gets the latest ID, and so does the Next Query that follows it.

Link to comment
Share on other sites

Thanks! But here's the thing ... What if I want to specify the ID #? How would I narrow the search by the ID # first ... Then by the date? Thanks for your patience!

 

<?php 

$query = mysql_query("SELECT * FROM `table` WHERE `id` = '1' ");

// OR


$id = mysql_real_escape_string(strip_tags($_GET['id']));

$query = mysql_query("SELECT * FROM `table` WHERE `id` = '$id' ");


?>

Link to comment
Share on other sites

He said there are mutiple IDs with the same number.  Nothing said they were unique.

 

He asked a specific question

 

How would I narrow the search by the ID # first ... Then by the date?

For example if I want ID #5 ... there are muliples entries for the ID ... So I need to find the newest or latest entry ...

 

 

Link to comment
Share on other sites

He said there are mutiple IDs with the same number.  Nothing said they were unique.

 

He asked a specific question

 

How would I narrow the search by the ID # first ... Then by the date?

For example if I want ID #5 ... there are muliples entries for the ID ... So I need to find the newest or latest entry ...

 

 

 

Oh i didn't read it properly, but this would mean he has a AUTO INCREMENT field with a second ID field.

Link to comment
Share on other sites

LOL DUH ... I'm sorry for the stupid follow up ...

 

 I am obviously still learning PHP & MySQL and the one thing I always find is that I go through and find a very long way to accomplish something that PHP could do in one line of code. HAHA .. Thanks again for everything!

Link to comment
Share on other sites

I actually simplified the fields a bit ... that table has 15+ fields. The ID number I was speaking of is not actually an ID number but a store # that's stored in another DB and is stored in a session. I wasn't sure what the MYSQL query would look like is all. Thanks again and have a GREAT weekend!

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.