• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active Records and MySQL Keywords like NOW()

#1
[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

#2
[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())");

#3
[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);

#4
[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');

#5
[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.

#6
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.