Recording time stamp

Reply

Join Date: Feb 2005
Posts: 20
Reputation: Stick is an unknown quantity at this point 
Solved Threads: 0
Stick Stick is offline Offline
Newbie Poster

Recording time stamp

 
0
  #1
Apr 6th, 2005
I have a database that I would like to keep track of who and when people are logging on. I created a table called users. It has three fields, id, user, date. I created this script that is executed when they log in.
<?php

$db = mysql_connect("","","");

mysql_select_db("",$db);

$today= getdate();

$query = "INSERT INTO users VALUES ('','$user','$today')";
mysql_query($query);


?>

The script enters the user ok, but enters a zero value for the date and time.

If I add print_r($today); to the script, it returns the correct date and time to the browser, but not the database.



Thanks in advance for any help!

Stick
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 360
Reputation: Gary King will become famous soon enough Gary King will become famous soon enough 
Solved Threads: 5
Team Colleague
Gary King's Avatar
Gary King Gary King is offline Offline
PHP/vBulletin Guru

Re: Recording time stamp

 
0
  #2
Apr 10th, 2005
$today becomes an array, so it itself has no value

What I suggest is to turn $today from an array to a string by doing this:

[php]$today = serialize($today); // turns into a string[/php]

Then insert $today into the database like you normally would. In order to get the data back from $today, just unserialize before using, like so:

[php]$user = mysql_fetch_array(mysql_query("SELECT * FROM users");

$today = unserialize($user['today']);

// $today is now useable again!

[/php]
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 20
Reputation: Stick is an unknown quantity at this point 
Solved Threads: 0
Stick Stick is offline Offline
Newbie Poster

Re: Recording time stamp

 
0
  #3
Apr 11th, 2005
Gary,
I updated my script as follows.

db = mysql_connect("","","");

mysql_select_db("",$db);

$today= getdate();

$today = serialize($today);

$query = "INSERT INTO users VALUES ('','$user','$today')";
mysql_query($query);

It still gives me a zero value in the table.
I have the field type set to timestamp. Is this correct?

Thanks for your help!

Stick
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 360
Reputation: Gary King will become famous soon enough Gary King will become famous soon enough 
Solved Threads: 5
Team Colleague
Gary King's Avatar
Gary King Gary King is offline Offline
PHP/vBulletin Guru

Re: Recording time stamp

 
0
  #4
Apr 11th, 2005
Field type should be something like TEXT.

If you want timestamp, then you need a UNIX timestamp. So do something like [php]$query = "INSERT INTO users VALUES ('','$user',time())";[/php]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC