Disable timestamp update in codeigniter - php

Faced the problem: when i am trying to update field via active record in CodeIgniter there also updates timestamp field. For example my code:
$this->db->where('user_i', 1);
$this->db->update('ci_users', $user_info);
Creates this sql:
UPDATE `ci_users` SET `user_fullname` = 'asdasdfsafd' WHERE `user_iD` = 1
But after execution there also changes ci_users.user_creation_date field. Where there may be a problem?

That's what timestamp fields are for.
If you don't want them to be altered on update - use DATETIME instead

Mysql will automatically update the timestamp to current-timestamp if you have give default value CURRENT_TIMESTAMP or NULL to the column, so the solution is that you should not give the default value to that column...

Related

How to insert data dynamically in MySQL

I need to record the date each event happens. I have this table.
Click here to view the table
NULL spaces are available to save a new date.
The X represents the date that the event occurred.
The problem is I do not know how to update the date each event occurs
I need to know the best option if you use INSERT or UPDATE querys.
Thank you for your help
If you just want to update a column in a table with the current time:
UPDATE `yourtable` SET `yourcolumn` = NOW() WHERE `ID` = yourid
Assuming your columns are DATETIME() columns.
As an aside, it's best to have event-related information in another relationship table. This way you can link multiple events for each row in your main table. This provides a more accurate data-trail for accounting purposes (or in other words, you can see each and every update without overwriting anything).
If you are seeking to have a 'last_modified' column on your table to help you keep track of changes made on your records, you should do this:
ALTER TABLE my_table
ADD last_modified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Basically what it does is to add a 'last_modified' column, Set it to be a timestamp and make it to be updated with the current timestamp when there is an update on the record.

redbeanphp : How to use mysql ON UPDATE timestamp

I need mysql to automatically update a field on every update. I created the field as:
`lastModified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
But the problem is as I update a record using R::store, the lastModified does not get updated automatically but is set to the last set value. Note that my code is not setting the field. Basically doing a findOne and store to set any other field should update the timestamp but looks like redbeans is reading the whole row (so lastUpdated is filled in as old time) and assuming when I store, it thinks I am setting it to old time value.
lastModified property is pure MYSQL. It does not depend on the implementation.
RedbeanPHP also updates mysql database, have you disabled this?
R::freeze( TRUE );
Once you put it on freeze, you can modify the database and tables yourself, and redbean will not update it. This should also take care of the lastModified property.
try this
$bean->lastModified = date('Y-m-d G:i:s');
R::store($bean);

SQL Database Timestamp

I have a database where people register on a website and their form data then gets inserted into their database. Along with all that information, I want to insert the time of their registration. For example I have an auto-incrementing ID column which I do nothing in my php script for, the sql database automatically increments that with every new entry. In the same way, can I have a time column that I don't have to do anything in the script for, rather the database will just get the current time and put that along with the other inserted information? I'm using phpMyAdmin and I tried adding a column named Time with the type as DATETIME and the default value as the CURRENT_TIMESTAMP but it woulnd't let me add that and said invalid default value for Time.
Change the column type to TIMESTAMP.
Use a timestamp field, which does exactly what you want.
use timestamp column type e.g.
`inserted` timestamp NOT NULL default CURRENT_TIMESTAMP

How to get time since MySQL database last edited in PHP

I need to know the time since a table in my MySQL database was edited. Is there any way to do this in PHP? The only way I can think of is to get the update time, and compare it to the current time (which will be a little bothersome).
SELECT TIMEDIFF(CURRENT_TIME, UPDATE_TIME)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'my_table'
One solution would be to add a timestamp field, that is automatically updated whenever a row is changed.
Then you can find the last change time by selecting the last update value:
# Add a timestamp column:
ALTER TABLE [TABLENAME] add column `ts_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP;
# Get the last update value:
SELECT MAX(ts_update) from [TABLENAME];
It's a bit of a pain since SHOW TABLE STATUS doesn't seem to return a standard result set. Every way I tried to use it as a subquery failed. It seems like you'll need to figure it out programmatically.
SHOW TABLE STATUS
WHERE name = 'target_table';
And if you're concerned about the timezone just do a separate SELECT NOW() to get the time the mysql server has.
Note: This will only show you the time at which the table schema was updated. If you want to know when the last time a row was inserted/edited you'll have to add a timestamp column like Amirshk suggested.

Submitting current timestamp in CakePHP

What is the method to submit a current timestamp directly on an INSERT or an UPDATE? If I were running regular SQL, I would use the function NOW() for the specific SQL field on submission. How would I do that with CakePHP?
$this->Model->save($this->data)
In CakePHP, you can include the NOW() function unescaped by using DboSource::expression
$this->data['SomeModel']['your_datetime_field'] = DboSource::expression('NOW()');
$this->Model->save($this->data);
This is the preferred way of including MySQL functions in your saves.
http://api.cakephp.org/2.3/class-DboSource.html#_expression
if you add the created and modified columns in you table they will be automatically populated with current time stamp. If the case is different - i.e. you want to populate a field which later on you want to modify, probably using the edorian's solution is best.
You can set timestamp field to auto initialize and auto update
timestampfield TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

Categories