954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recording time stamp

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

Stick
Newbie Poster
20 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

$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]

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 

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

Stick
Newbie Poster
20 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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]

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You