Welcome Guest, Not a member yet? Register   Sign In
Insert Multiple Values on a single query
#1

[eluser]guidorossi[/eluser]
Hi!

I'm wondering if there is any way to do inserts of multiple values on a single query using active record.

Something like:
Code:
INSERT INTO people (name, age)
  VALUES
  ('Juan', 24),
  ('Pedro', 21),
  ('Jose', 22),
  ('Maria', 25),
  ('Roberto', 29)

I've tried with a loop like:

Code:
$data = array(
                           'name' => $name[$i] ,
                           'age' => $age[$i]
                        );
and then the
Code:
$this->db->insert('people', $data);

This works, but it seems to make each values insert on a new query.

EDIT
This isn't really working, its only inserting the last item of the array, I think it should be something like:

Code:
$data[$i] = array(
                           'name' => $name[$i] ,
                           'age' => $age[$i]
                        );

but with this code I get:
Quote:A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysqli/mysqli_driver.php

Line Number: 536
#2

[eluser]SPeed_FANat1c[/eluser]
With transanctions you could use multiple queries but result would be like you use one query - if any of them fails, everything is undone back to original database state. I anwered how to use them there:

http://ellislab.com/forums/viewthread/177655/
#3

[eluser]Cristian Gilè[/eluser]
Hi guidorossi,

what you need is a method to facilitate easy bulk inserts into a given table.

There is a wiki page about this topic.

Cristian Gilè
#4

[eluser]guidorossi[/eluser]
Thanks Cristian

I'm already implementing something like that wiki says, but I thought that maybe CI has this function incorporated and I was missing something




Theme © iAndrew 2016 - Forum software by © MyBB