PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 1:27 am
I have a table something like below:
Code: Select all
user1 green approved
user2 black approved
user1 black pending
user1 green approved <------- this row
user2 black pending
How should I write the query to call the data on that row. In other word is to call the
user1 latest data (row) on that table.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Sep 06, 2004 2:09 am
is there any other data to sort by?
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 2:13 am
feyd wrote: is there any other data to sort by?
what do you mean?
There is other fields like userID, name, unit and so on but all the data are same for each user everytime the data is store into database.
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Mon Sep 06, 2004 4:16 am
This db layout doesn't seem very organized. Either that or your explaination is a bit off. Either that or I need some sleep.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 4:22 am
Ok. I think this is more easy to understand.
If we have duplicate data in one table, how can we write the query to call the latest one.
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Mon Sep 06, 2004 4:23 am
Timestamp the entires. Or go by highest userID/entryID or whatever.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 4:34 am
Sami wrote: Timestamp the entires.
How to use timestamp?
I ask the user to enter the date and it will be store in db in this format
yyyy-mm-dd
can I use this info to call the latest data. How the query will look like?
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Mon Sep 06, 2004 4:59 am
You should not allow the user to input the timestamp.
If you are using MySQL use NOW() instead.
This way if you SELECT the rows, you can ORDER them BY timestamp DESC and LIMIT the resultset to 1 row
jason
Site Admin
Posts: 1767 Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:
Post
by jason » Mon Sep 06, 2004 6:59 am
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 9:13 pm
That means, I just need to add another field for timestamp. But how can I insert the value into that field?? Never use timestamp before..
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Mon Sep 06, 2004 9:37 pm
Well when in doubt about something, Google it.
It would lead you to a page like the one jason linked to, or this one: [php_man]time[/php_man]()
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Mon Sep 06, 2004 10:08 pm
Sami wrote: Well when in doubt about something, Google it.
I have read and google it
Sami
But I still cannot understand it. Most of the explaination are too confusing
It doesn't show any simple example for me to understand.
I need to know how the timestamp will be inserted to the field that I created and how to call the latest data in my query.
Can you show me the example of the query or just suggest another link for me to google it
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Sep 06, 2004 10:15 pm
try here: [mysql_man]date time function[/mysql_man]you'll potentially want [mysql_man]UNIX_TIMESTAMP[/mysql_man] and [mysql_man]NOW[/mysql_man] to insert the data.. and simple ORDER BY field_name DESC to get the newest ones first.
Draco_03
Forum Regular
Posts: 577 Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada
Post
by Draco_03 » Tue Sep 07, 2004 12:55 pm
I would suggest you to create a primary key auto increment (id) AND a timestamp.