echo the very last row in one column SQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

echo the very last row in one column SQL

Post by fresh »

Hey,

was wondering how I would echo to my page the very last row - field of my reg date column... I would like to echo the date and their username, but only the last entry, since that would represent the newest member... thanks :)




edited by : Infolock
*** Moved to database section ***
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

SELECT * FROM table ORDER BY date_joined_field LIMIT 1
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hmm

Post by fresh »

not working, here's my code:

Code: Select all

<?php
$dat = "SELECT * FROM users ORDER BY reg_date LIMIT 1";
$datum = mysql_query($dat);

echo "".$datum."";

?>
it isn't echoing anything at the moment... thanks :)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

It should be echoing "Resource ID x" or something along those lines..
Also, if all your echoing is a variable, theres no need to use "".$var."".

Try this (i'm assuming your column names are username and date):

Code: Select all

<?php
$sql = "SELECT * FROM users ORDER BY reg_date LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "Date: " . $row['date'] . "<br />";
echo "Username: " . $row['username'];
?>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post by fresh »

thanks alot for that script, it has actually been the closest to success that I have had with this feature I have been trying to build... basically it is the newest member feature, which takes the very last entry and displays when they regisered and what their name is.. however, this script takes from the top, how would I take from the bottom, thanks :)
jtc970
Forum Commoner
Posts: 38
Joined: Sun Jan 18, 2004 11:49 pm

Post by jtc970 »

Code: Select all

$sql = "SELECT * FROM users ORDER BY -reg_date LIMIT 1";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh..

Code: Select all

ORDER BY reg_date DESC
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

alright

Post by fresh »

thanks alot guys, it started working now, it's not perfect, but it's good enough for me, so thanks again, I appreciate your help :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lol whats not perfect about it?

Doing it that way is fast, simple, and dont require alot of memory.

what more/ess to make it 'perfect?'

:wink:
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yeah, its certainly the way i would always do it. Sorry that i missed the DESC off my first post btw :P
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

well, I said it's not perfect only because it doesn't output a true reading, but it is close enough for me.. ;)
Post Reply