CodeIgniter Forums
is there any way to pass mysql's NOW() to mysql ? - 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: is there any way to pass mysql's NOW() to mysql ? (/showthread.php?tid=6531)



is there any way to pass mysql's NOW() to mysql ? - El Forum - 03-03-2008

[eluser]soupdragon[/eluser]
trying to use active record

is there any way to pass mysql's NOW() to mysql ?
$insertarray['id'] = '';
foreach($sellerdata as $what => $details) {
if($what != 'passconf') {
$insertarray[$what] = $details;
}
}
$insertarray['confirmed'] = 'no';
$insertarray['paid'] = '';
$insertarray['datepaid'] = '';
$insertarray['lastlogin'] = NOW();
$insertarray['login_count'] = '0';
$insertarray['status'] = 'dead';
if($this->db->insert('sellers',$insertarray)) {


if not whats a viable alternative - not wanting to rely on php's time/date functions
thanks


is there any way to pass mysql's NOW() to mysql ? - El Forum - 03-03-2008

[eluser]xwero[/eluser]
You can try to single out the field
Code:
foreach($sellerdata as $what => $details) {
if($what != ‘passconf’) {
$insertarray[$what] = $details;
}
}
$insertarray[’confirmed’] = ‘no’;
$insertarray[’paid’] = ‘’;
$insertarray[’datepaid’] = ‘’;
$insertarray[’login_count’] = ‘0’;
$insertarray[’status’] = ‘dead’;

$this->db->set('lastlogin', 'NOW()',false);
$this->db->set($insertarray);
$this->db->insert('sellers');



is there any way to pass mysql's NOW() to mysql ? - El Forum - 03-03-2008

[eluser]soupdragon[/eluser]
stunning response time !

and works perfectly :-)

thanks


is there any way to pass mysql's NOW() to mysql ? - El Forum - 04-22-2008

[eluser]Ignacio[/eluser]
Yup, that works for me too but...

Code:
//Data
$data = array(
    'ID' => '',
    'Hash' => $params['file_name'],
    'DateUploaded' => 'NOW()'
);    

//Insert
$this->db->insert('Photos', $data);

//Idea produces:
//INSERT INTO Photos (ID, Hash, DateUploaded) VALUES ('', '326248800480e3fde00ea4', 'NOW()')

It doesn't work, but it should, don't you think?


is there any way to pass mysql's NOW() to mysql ? - El Forum - 04-22-2008

[eluser]gtech[/eluser]
I think when you insert values using the active record quotes are escaped.. the purpose of the $this->db->set('DateUploaded', 'NOW()',false); is to ensure 'NOW()' is not escaped (the false parameter tells the set not to escape 'NOW()').

does this work?
Code:
//Data
$data = array(
    'ID' => '',
    'Hash' => $params['file_name']
);  

$this->db->set('DateUploaded', 'NOW()',false);
$this->db->set($data);

$this->db->insert('Photos');



is there any way to pass mysql's NOW() to mysql ? - El Forum - 04-22-2008

[eluser]Ignacio[/eluser]
Yes, I'm using that way right now and it works fine, but like I said, it "should" work in the other way too.


is there any way to pass mysql's NOW() to mysql ? - El Forum - 04-22-2008

[eluser]Pascal Kriete[/eluser]
It doesn't work for the exact reason that gtech mentioned. Insert is always escaped for security reasons.


is there any way to pass mysql's NOW() to mysql ? - El Forum - 04-22-2008

[eluser]gtech[/eluser]
when you pass your params through the insert function all the quotes have slashes added to them as the set() function is called within the insert() function with escape set to TRUE.

it would be the same as doing:
Code:
//Data
$data = array(
    'ID' => '',
    'Hash' => $params['file_name']
);  
//use true instead and value will be inserted as \'NOW()\' meaning 'NOW()' will be inserted into the database.
$this->db->set('DateUploaded', 'NOW()',TRUE);
$this->db->set($data);

$this->db->insert('Photos');

thats why it wont work, active record is built to escape strings as default.