CodeIgniter Forums
CURDATE() and INTERVAL in codeigniter - 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: CURDATE() and INTERVAL in codeigniter (/showthread.php?tid=49193)

Pages: 1 2 3 4


CURDATE() and INTERVAL in codeigniter - El Forum - 02-10-2012

[eluser]Thiago Leao[/eluser]
Hi friends, I have a question.

I have this query:

Code:
INSERT INTO buy (id_products, date_start, date_finish) VALUES ('1', CURDATE(), CURDATE() + INTERVAL 30 DAYS)

I want this query in codeigniter, as I do?

I tried it but did not work:

Code:
function insertBuy($id_products){
  $this->load->helper('date');
  $array = array('id_products' => $id_products, 'date_start' => CURDATE(), 'date_finish' => 'CURDATE() + INTERVAL 30 DAYS');
  $this->db->set($array);
  $this->db->insert('buy');
}

thank guys!



CURDATE() and INTERVAL in codeigniter - El Forum - 02-10-2012

[eluser]Bhashkar Yadav[/eluser]
Code:
$this->db->insert('buy', $array);



CURDATE() and INTERVAL in codeigniter - El Forum - 02-10-2012

[eluser]InsiteFX[/eluser]
He's using db->set->($array) So you do not need to pass it to the insert method.



CURDATE() and INTERVAL in codeigniter - El Forum - 02-10-2012

[eluser]Bhashkar Yadav[/eluser]
thanks InsiteFX.
well, i think here is error :
Code:
$array = array('id_products' => $id_products, 'date_start' => CURDATE(), 'date_finish' => 'CURDATE() + INTERVAL 30 DAYS');

and CURDATE() should also be within quotes.
Code:
$array = array('id_products' => $id_products, 'date_start' => 'CURDATE()', 'date_finish' => 'CURDATE() + INTERVAL 30 DAYS');
As CURDATE() is mysql function not PHP.


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Thiago Leao[/eluser]
[quote author="Bhashkar" date="1328940458"]thanks InsiteFX.
well, i think here is error :
Code:
$array = array('id_products' => $id_products, 'date_start' => CURDATE(), 'date_finish' => 'CURDATE() + INTERVAL 30 DAYS');

and CURDATE() should also be within quotes.
Code:
$array = array('id_products' => $id_products, 'date_start' => 'CURDATE()', 'date_finish' => 'CURDATE() + INTERVAL 30 DAYS');
As CURDATE() is mysql function not PHP.[/quote]

Hi friend, i had already done so, and still did not work!
=/


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]InsiteFX[/eluser]
You can try this, but then you will need to use set on all the fields.
Code:
$this->db->set('date_finish = DATE_ADD(CURDATE(), INTERVAL 30 DAY)');

// or
$array = array('id_products' => $id_products, 'date_start' => CURDATE(), 'date_finish' => 'DATE_ADD(CURDATE() + INTERVAL 30 DAYS)');



CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
How to print string SQL created by Active Record class?
Maybe you need to print your SQL and run it.


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Anyway, i use SQL Strings like:

Code:
$sql = 'select * from user where iduser = ?';
$query = $this->db->query($sql, $iduser);

I just use Active Record Class (CI string builder) when i dont have any parameters.
Active Records escapes strings, but i think it don't bind parameters.


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]InsiteFX[/eluser]
Quote:How to print string SQL created by Active Record class?
Maybe you need to print your SQL and run it.

CodeIgniter User Guide - Query Helper Functions

See:
Code:
$this->db->insert_string();
$this->db->update_string();



CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Thank you, Insite.