Welcome Guest, Not a member yet? Register   Sign In
Active Record Update: using NOW() and other SQL
#1

[eluser]suntrop[/eluser]
I want to insert the current time and increment one field.

Code:
$data = array(
   'last_login' => "NOW()",
   'login_count' => "login_count + 1"
   );
$this->db->where('id', $user_id);
$this->db->update('users', $data);

… but I get an error, because CI produces this code:
Code:
UPDATE `p_users` SET `last_login` = 'NOW()', `login_count` = 'login_count + 1' WHERE `id` = '1'

I read that I can prevent escaping for INSERT. But what about UPDATE?

What I am doing wrong?
#2

[eluser]pmoroom[/eluser]
[quote author="suntrop" date="1309646823"]I want to insert the current time and increment one field.

Code:
$data = array(
   'last_login' => "NOW()",
   'login_count' => "login_count + 1"
   );
$this->db->where('id', $user_id);
$this->db->update('users', $data);

… but I get an error, because CI produces this code:
Code:
UPDATE `p_users` SET `last_login` = 'NOW()', `login_count` = 'login_count + 1' WHERE `id` = '1'

I read that I can prevent escaping for INSERT. But what about UPDATE?

What I am doing wrong?[/quote]

Go here:
http://ellislab.com/codeigniter/user-gui...ecord.html

Search for "$this->db->set();" and have a quick read.

Something like this should work:

Code:
$this->db->set('last_login', 'NOW()', FALSE);  
$data = array(  
   'login_count' => "login_count + 1"
   );
$this->db->where('id', $user_id);
$this->db->update('users', $data);
#3

[eluser]InsiteFX[/eluser]
Code:
$now = date("H:i:s");

$data = array(
   'last_login' => $now"
   );
$this->db->where('id', $user_id);
$this->db->update('users', $data);

InsiteFX
#4

[eluser]suntrop[/eluser]
Thanks! I thought there is a way I can use the array as well.




Theme © iAndrew 2016 - Forum software by © MyBB