How to create auto updated/modified and created columns of a table in mysql


Your table should look like:


CREATE TABLE `new_table` (
  `id` bigint(20) NOT NULL auto_increment,  
  `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `created` timestamp NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`)
)

To get the modified/created column changes you need to pass null value while updating or inserting values in a table. Please see the below example to make sense


/* Insert query: */

$pdata = array(	
	'modified' => null,
	'created' => null
);
$val = $this->db->insert('new_table', $pdata);

/* update query:  */

$pdata = array(
	'modified' => null
);
$this->db->where('id', $id);
$val = $this->db->update('new_table', $pdata);


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s