Welcome Guest, Not a member yet? Register   Sign In
execute several queries, batch queries.
#1

[eluser]jorgeakanieves[/eluser]
I have to do several inserts into the same table. I don´t want to get it using a for bucle. I thinks it´s possible to do this in the same transaction.

how could I do?

It´s not valid:

Code:
$query ="INSERT INTO table(field1,field2,field3) VALUES ('16','2','5');INSERT INTO table(field1,field2,field3) VALUES ('16','3','4');INSERT INTO table(field1,field2,field3) VALUES ('16','4','3');INSERT INTO table(field1,field2,field3) VALUES ('16','5','2');INSERT INTO table(field1,field2,field3) VALUES ('16','6','1');"

$query = $this->db->query($sql);
return $query->result();
#2

[eluser]Phil Sturgeon[/eluser]
This gives you an error? That's not because you are missing a ; at the after the query string is it?

Anyway to answer the question, if you are only trying to only do multiple INSERT's, then use the following structure for your queries.

Code:
$query ="INSERT INTO table(field1,field2,field3) VALUES
('16','2','5'),
('16','3','4'),
('16','4','3'),
('16','5','2'),
('16','6','1');";

$query = $this->db->query($sql);
return $query->result();
#3

[eluser]jorgeakanieves[/eluser]
Yes!!! that´s right!! thanks!!
#4

[eluser]obiron2[/eluser]
Remeber though that each one is a separate commit to the database. If you need them to all succeed or all fail you will need to wrap them in a transaction.

Obiron
#5

[eluser]jorgeakanieves[/eluser]
I know but i´m sure they will be done because there´s no foreign keys or similar. That´s a simple insert.




Theme © iAndrew 2016 - Forum software by © MyBB