Jump to content


Photo

Help needed


  • Please log in to reply
6 replies to this topic

#1 RSprinkel

RSprinkel
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 07 September 2006 - 06:15 PM

Hi all,

I am trying to figure out a code to pull the newest member out my user database.

I am using a MySQL db.

Any help would be greatly appreciated.

Thanks much in advance.
RSprinkel
A PHP/MySQL NOVICE I AM

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 06:17 PM

Use this as the query:
SELECT * FROM users_table ORDER BY user_id_column DESC LIMIT 1
Should select the last row from the database

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 September 2006 - 08:38 PM

Use this as the query:
SELECT * FROM users_table ORDER BY user_id_column DESC LIMIT 1
Should select the last row from the database


assuming your table is set up with an auto-incrementing primary key ;-) lol

you really will have to give us a little more information than that. if you have an id column that is an auto-increment field, that's probably your best bet. however, if you have a date column that records the membership date, something like that could be used as well. if you don't have any sort of ordering available to you, i'm afraid you might be stuck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 R_P

R_P
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 07 September 2006 - 08:56 PM

If you do not have an auto-incrementing primary key or date column (which would be bad practice), getting info will be much more process heavy. Since MySQL by default orders data by date inserted (ASC), I guess you could (theoretically, mind you) select ALL the usernames into an array, and then use array_pop() to get the last username.

Just an idea.
Pro in: Win2K3S | Apache2 | PHP5 | Perl5 | MySQL | MSSQL | Firefox | Photoshop
Student of: Ubuntu6 | Java | C | VB.NET | ASP.NET
Developer: Roddzilla Webstudios, Burrson CG, DVIDSHUB, The Four Nations
Student: Georgia Tech, Georgia Tech College of Computing

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 08:58 PM

Well ideally any table should have an auto incrementing column, which will be the primary key. Howev i was pressed for time when I was posting, so I just rushed the post abit.

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 08 September 2006 - 12:36 PM

Well ideally any table should have an auto incrementing column, which will be the primary key. Howev i was pressed for time when I was posting, so I just rushed the post abit.

i figured as much ;) so i thought i'd clarify for you...

one other thought... i'm not sure how mysql handles this, but in postgresql, you have a pid that is generated as a bookmark for the server, and you never even see it (even through phpmyadmin) unless you specifically query for it. i suppose if mysql has something similar, you could order by that as well.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 08 September 2006 - 12:49 PM

Oracle has it too... 'rownumber' it's a psuedocolumn.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users