CodeIgniter Forums
Active Records and MySQL Keywords like NOW() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Records and MySQL Keywords like NOW() (/showthread.php?tid=26126)



Active Records and MySQL Keywords like NOW() - El Forum - 01-06-2010

[eluser]Unknown[/eluser]
Hi there,

am I right that there is no possible way to insert a dataset with active records that contains NOW() for a datetime field?

Active Records is quoting it automatically Sad

Active Records: Insert into bla(mydatetimefield) values('NOW()')
Correct way: Insert into bla(mydatetimefield) values(NOW())


I would be glad to hear about a solution Smile
Thanks alot!

Nils


Active Records and MySQL Keywords like NOW() - El Forum - 01-06-2010

[eluser]saidbakr[/eluser]
Hello,
I think that using SQL function such as NOW() in active record is not possible. However, you are able the method query() of the db object
Code:
$this->db->query("INSERT INTO my_table (title,the_date) VALUES (\"$title\",NOW())");



Active Records and MySQL Keywords like NOW() - El Forum - 01-06-2010

[eluser]Eric Barnes[/eluser]
You can also set the date in a variable and then use it.
IE:

Code:
$data['date_field'] = time();
$this->db->insert('table', $data);



Active Records and MySQL Keywords like NOW() - El Forum - 01-06-2010

[eluser]Armchair Samurai[/eluser]
Use the set function and set the third parameter to FALSE to keep CI from escaping the values.

Code:
$this->db->set('mydatefield', 'NOW()', FALSE);
$this->db->insert('bla');



Active Records and MySQL Keywords like NOW() - El Forum - 01-07-2010

[eluser]saidbakr[/eluser]
[quote author="Armchair Samurai" date="1262854942"]Use the set function and set the third parameter to FALSE to keep CI from escaping the values.

Code:
$this->db->set('mydatefield', 'NOW()', FALSE);
$this->db->insert('bla');
[/quote]

Very informative answer.


Active Records and MySQL Keywords like NOW() - El Forum - 01-07-2010

[eluser]Unknown[/eluser]
Thanks for all the input!

I think
Code:
$this->db->set('mydatefield', 'NOW()', FALSE);
..seems to be the best solution.

cheers
Nils