Welcome Guest, Not a member yet? Register   Sign In
CURDATE() and INTERVAL in codeigniter
#1

[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!
#2

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

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

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

[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!
=/
#6

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB