CodeIgniter Forums

Full Version: is there any way to pass mysql's NOW() to mysql ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

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

El Forum

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

and works perfectly :-)

thanks

El Forum

[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?

El Forum

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

El Forum

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

El Forum

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

El Forum

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