Jump to content

[SOLVED] Retrieving last login from activity log table


TGWSE_GY

Recommended Posts

I have a table in my database called ActivityLog and it holds the following data CustID, StartTime, Date, EndTime, and IPAddress.

 

 

Well there can be multiple records with the same CustID, what I am wondering is if there is a way to grabbing the most recent record in the table after I pull all of the records with CustID. Then display to the user by echoing?

 

Thanks

Link to comment
Share on other sites

assuming 'Date' is what constitutes having the "most recent":

 

select * from ActivityLog where CustID = '$custID' order by `Date` desc limit 1

 

notice the backticks on `Date`.  That's because date is a reserved word.  If you want to make a column name a reserved word, you must put backticks around it.  Also, that only works in mysql. So it's not going to work in some other database.  I suggest you change that column name to something else, as it's bad programming to use reserved words for things.

Link to comment
Share on other sites

Right. But how would you get the most recent entry for CustID?

 

I have this in mind -

SELECT * FROM ActivityLog WHERE CustID = '3' ORDER BY id DESC LIMIT 1

 

It's possible we're thinking about this in 2 different perspectives.

Link to comment
Share on other sites

Well I posted my take already, see the bolded red:

 

assuming 'Date' is what constitutes having the "most recent":

 

select * from ActivityLog where CustID = '$custID' order by `Date` desc limit 1

 

notice the backticks on `Date`.  That's because date is a reserved word.  If you want to make a column name a reserved word, you must put backticks around it.  Also, that only works in mysql. So it's not going to work in some other database.  I suggest you change that column name to something else, as it's bad programming to use reserved words for things.

 

Ordering by the "id" (I assume this is where you mean to have the primary key, or at least some kind of globally incremented value) would show the most recent one entered, but it would not tell you when it was entered.  Sure, when you're selecting *, all those date fields will be selected, so he will have that info, but the point is, if his goal is to find the most recent entry, ordering by some "id" is not necessary. Just order by the column that constitutes having the "most recent date" (I'm assuming it's the `Date` column he has).

Link to comment
Share on other sites

I have a table in my database called ActivityLog and it holds the following data CustID, StartTime, Date, EndTime, and IPAddress.

 

 

Well there can be multiple records with the same CustID, what I am wondering is if there is a way to grabbing the most recent record in the table after I pull all of the records with CustID. Then display to the user by echoing?

 

Thanks

 

I interpreted the part in red as "when".  But even still, a column with timestamp (or a date type column acting in same principle) would act as your incremented column just the same.

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.