Printing out when the user last logged in

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
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

Printing out when the user last logged in

Post by emilia »

Hey

I am having some trouble printing out when the user last logged in
I am trying to extract the date from the database and then update the lastlogin with the current date in the database! When I try to print the the date nothing is printed! I would love any help at all!
Thanks :)

Sorry here is the code!

//Set date
$date = date("d-m-y");


$result1= mysql_query(" SELECT Lastlogin FROM Users WHERE username='$username'") or die ("couldn't execute query.");
while ($row = mysql_fetch_array($result1)) {
echo "You last logged in on the ";
echo $Row[0];
echo "Todays date is ";
echo $date;
}


//Update the lastlogin in the database with todays date
$update_login = ("UPDATE Users SET Lastlogin = '$date' WHERE username = '$username'");
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Is your field Lastlogin a varchar or is it a datetime field?
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

Post by emilia »

I am using a datetime! Whats the difference?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

MySQL datetime fields expect data that looks like "2003-12-31 23:59:59" so usually, you'd have to format the date before insertion.

with the way you're using the date, you could just use a varchar field, but that might cause problems if you need to compare dates.

See http://www.phpfreaks.com/mysqlref/3.php for info on date_format(), a MySQL function.

Or http://www.mysql.com/doc/en/DATETIME.html for more info on datetime fields.
emilia
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2003 12:45 pm

Post by emilia »

Hey thanks for your help! I changed the field to a varchar but its still not working I don't understand why? Ive being at this for ages and I just can't get it to work.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

$result1= mysql_query(" SELECT Lastlogin FROM Users WHERE username='$username'") or die ("couldn't execute query.");
while ($row = mysql_fetch_array($result1)) {
echo "You last logged in on the ";
echo $Row[0];
echo "Todays date is ";
echo $date;
}
I can't remember if variable names in PHP are case-sensitive. They might be, and if that's the case, then you should change echo $Row[0]; to echo $row[0]; , or better yet echo $row["Lastlogin"]; .
Post Reply