944,168 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7448
  • PHP RSS
Apr 6th, 2005
0

Recording time stamp

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stick is offline Offline
20 posts
since Feb 2005
Apr 10th, 2005
0

Re: Recording time stamp

$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]
Team Colleague
Reputation Points: 53
Solved Threads: 5
PHP/vBulletin Guru
Gary King is offline Offline
360 posts
since Nov 2003
Apr 11th, 2005
0

Re: Recording time stamp

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stick is offline Offline
20 posts
since Feb 2005
Apr 11th, 2005
0

Re: Recording time stamp

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]
Team Colleague
Reputation Points: 53
Solved Threads: 5
PHP/vBulletin Guru
Gary King is offline Offline
360 posts
since Nov 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: serch mysql for many fields
Next Thread in PHP Forum Timeline: Writing vBulletin Hacks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC