Here is the table of interest when exported via phpMyAdmin:
CREATE TABLE IF NOT EXISTS `users` (
`ip` varchar(20) NOT NULL,
`lastcheck` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `ip` (`ip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Here is the query:
mysql_query("REPLACE INTO users SET ip = '$uip', lastcheck = '$tim'") or throwerror("part2 ".mysql_error());
$tim is set to be time();
Now for some reason lastcheck is still set as 0000-00-00 00:00:00.
Can anyone help? Thanks.
I am not sure if maybe I understand your problem, looking at column lastcheck declaration CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP that means when ever you update (ip = '$uip') this column an automatic date update will also happen lastcheck. Which mean you can write you update statment as follows :
REPLACE INTO users SET ip = '$uip';
That should also update the lastcheck field, and I think it's better to use the mysql date functions to store your date/time than writing them in php and saving them as string in the database...
try using date('Y-m-d H:i:s') in stead of time()
time() in php returns a unix timestamp and that isn't a valid insert for MyySQL datetime
hope this helps
Related
I have been making tables with a modified row which is time stamped.
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
I am seeking to rename this as bookedinand timestamp the day of entry into the database.
i have looked on the net for a solution at sites like http://www.w3schools.com/sql/func_curdate.asp
And cant seem to find a solution that works or is what i need.
I tried to edit it in the databse myself but with no luck. as you can tell im a extremity novice
Instead of w3schools you really should use the actual MySQL documentations. http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
alter table tablename change `modified` `bookedin` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Or maybe just
alter table tablename change `modified` `bookedin` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP;
I think the on update may just be part of the timestamp datatype. And, I think that altering the name of the table might cause all the rows to update their timestamp to the date you did it. So you might want to make a copy of the table and try it out first. I remember running into a problem like that once that made me switch from using TIMESTAMP to DATETIME.
my simple question is .. Is it possible to insert php time() value by default in a column of a MYSQL database so that everytime i don't need to insert it. I know MYSQL provide CURR_TIMESTAMP..but they are not stored as integer unlike the time function which gives the timestamp as integer..
Any help will be greatly appreciated... Thanks
Example:
drop table if exists file_events;
create table file_events (
id int unsigned auto_increment not null,
file_id int unsigned not null,
event_time int unsigned not null default UNIX_TIMESTAMP(), # the field that defaults to "now"
primary key(id),
constraint foreign key fk_file_events_to_sam_files (file_id) references files(id) on delete cascade
) ENGINE=InnoDB;
If you want to do it on a mysql bases you can always use triggers [ http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html ] just create a trigger to set the column of your choice to the current timestamp
You should just stick with the MySQL timestamp and convert it as needed. It is easy to do...
strtotime("put the mysql timestamp here");
will produce the same format (unix timestamp) as
time();
Unlike many other DBMS, MySQL doesn't support functions as default values. The only exception is the one you mention: dates accept CURRENT_TIMESTAMP (or one of their values). It appears to me that's quite an acceptable solution anyway since you can easily convert your Unix timestamp on-the-fly:
SELECT foo_id, foo_info
FROM foo
WHERE FROM_UNIXTIME(1294133369)<=foo_date
Or, from PHP:
$sql = "SELECT foo_id, foo_info
FROM foo
WHERE '" . date('Y-m-d H:i:s', 1294133369) . "')<=foo_date";
Whatever, if you absolutely need to store dates as integers, you must write your own triggers. Here's a nice example:
http://mysqldatabaseadministration.blogspot.com/2006/01/playing-with-triggers.html
This seems like a really simple one but I'm struggling to figure it out. I want a column in my database that lists when a record was first created and another column that says when it was updated. It's my understanding I should be able to do all this just using MySQL. All help is appreciated :)
This stinks still no answer, reasons I'm already starting to miss Ruby on Rails...
You will probably need to use a combination of the Datetime datatype and the Timestamp data type. I would set my created column as a DateTime with a DEFAULT NOW(), and my updated column as a Timestamp with DEFAULT CURRENT_TIMESTAMP and an ON UPDATE CURRENT_TIMESTAMP attribute.
Here are the docs for the Timestamp dt:
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses, the column has the current timestamp for its default value, and is automatically updated.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP clause, the column has a default of 0 and is automatically updated.
With a constant DEFAULT value, the column has the given default and is not automatically initialized to the current timestamp. If the column also has an ON UPDATE CURRENT_TIMESTAMP clause, it is automatically updated; otherwise, it has a constant default and is not automatically updated.
To fulfill your question and for others viewing this question, here is the answer. Note this was written for MySQL 5.x.
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
`id` INT NULL AUTO_INCREMENT ,
`name` varchar(50) NOT NULL ,
`created` DATETIME ,
`updated` DATETIME ,
PRIMARY KEY (`id`),
INDEX (`name`)
);
DELIMITER $$
DROP TRIGGER IF EXISTS `test1_created`$$
CREATE TRIGGER `test1_created` BEFORE INSERT ON `test1`
FOR EACH ROW BEGIN
SET NEW.`created` = UTC_TIMESTAMP();
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DROP TRIGGER IF EXISTS `test1_updated`$$
CREATE TRIGGER `test1_updated` BEFORE UPDATE ON `test1`
FOR EACH ROW BEGIN
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DELIMITER ;
Note
You could use TIMESTAMP for the updated column which would have automatically updated the value thus not requiring the BEFORE UPDATE trigger, however TIMESTAMP has a range from 1970 to 2038 which is fast approaching and I like to think my applications will live forever :). Although TIMESTAMP is only 4bytes while DATETIME is 8bytes.
TIMESTAMP range '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
DATETIME range '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
From the MySQL 5.0 Certification Guide:
CREATE TABLE ts_test5 (
created TIMESTAMP DEFAULT 0,
updated TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data CHAR(30)
);
To control the initialization and update behaviour of a TIMESTAMP column, you add either or both of the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes to the column defintion when creating the table with CREATE TABLE...
and
...if you do not specify either of the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes when creating a table, MySQL automatically assigns BOTH to the first TIMESTAMP column
Also
you cannot use DEFAULT CURRENT_TIMESTAMP with one column and ON UPDATE CURRENT_TIMESTAMP with another
If you can't use the timestamp fields with default attributes that Paul W has suggested, you can use AFTER INSERT and AFTER UPDATE triggers to populate the fields.
You will need two fields "Created" and
"Updated" with type datetime. When a
new entry is inserted then insert
"Created" with current time stamp.
When a update is happening insert
"Updated" with the current time stamp,
and let the "Created" field remain as
it is.
For current time stamp you can use
NOW() in your mysql query.
i have a table that looks like this:
Field: msg_sent_datetime
Type: datetime
yet when i use NOW() in a php mysql insert query it is staying as all zeros?
any idea why?
If you're using it only at the time of INSERT, you could make the field a TIMESTAMP and set a default of CURRENT_TIMESTAMP.
Without knowing more about the code it's difficult to suggest much else.
Example with CURRENT_TIMESTAMP
CREATE TABLE example (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
msg TEXT,
msg_sent_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Also, if you want it to always update when there's a change to the row, you can add the ON UPDATE CURRENT_TIMESTAMP property to the table definition:
CREATE TABLE example (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
msg TEXT,
msg_sent_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Of course, all of this is totally dependent on switching to TIMESTAMP which you may not want
I dont knw exactly why, but mysql has 2 datetime functions: now() and sysdate(). Perhaps you can substitute now() with sysdate() to help you troubleshoot.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_sysdate
I have a database table in mysql with a field that is of "TIMESTAMP" type. I need help writing the SQL query to update the field with the current timestamp.
UPDATE tb_Test set dt_modified = ?????
Use:
UPDATE tb_Test
SET dt_modified = CURRENT_TIMESTAMP
WHERE ? -- if you don't specify, ALL dt_modified values will be updated
You can use NOW() instead of CURRENT_TIMESTAMP, but CURRENT_TIMESTAMP is ANSI standard so the query can be ported to other databases.
ALTER TABLE tb_Test MODIFY COLUMN dt_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
Now whenever any field is changed the dt_modified will be updated by the special trigger.