• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Codeigniter 2.1 - return ids after insert batch

How can I return all IDs after insert_bunch? Function:

public function insert_slike($id, $slike, $folder, $polje, $tabela)
{
        $slike = explode(',', $slike);
        $i = 1;
        $data = array();
        foreach ($slike as $slk) {
            $this->img_upload_resize($slk, $folder);
            $data[] = array(
                $polje => $id,
                'path' => $slk
                );
            $i++;
        }

        $this->db->insert_batch($tabela, $data);
}
      • 2
    • what will happen if some one run single insert along with batch insert? will get the right ID's? for example batch is running and ids from table comes like - 1,2,3,4,single insert here,6. Will it return - 1,2,3,4,6 or 1,2,3,4,5,6 on batch insert? making a reference to Alester answer, may be you know

Here is an efficient method:

Since your records have result ID's, I'm going to assume they also auto-increment.

If this is the case, you do this and still use insert_batch.

Here is what you do:

1. You take a count of the items you are inserting:

$count = count($data);

2. Run your batch insert:

$this->db->insert_batch($table, $data);

3. Get the first inserted ID of your batch:

$first_id = $this->db->insert_id();

4. Add the count (minus 1) to your insert ID to get the last records ID.

$last_id = $first_id + ($count-1);

There you go! You now have first and last ID's of your inserted records, and by extension, everything else in between.

Note:

A few people have asked what might happen if two batch inserts happen simultaneously; InnoDB will intelligently manage the ID's of the new rows to prevent the data intersecting, thus keeping this method valid.

  • 15
Reply Report
      • 2
    • @machineaddict that what I'm thinking for. What happens when 2 users insert two queries at the same time. Help?
      • 2
    • For 2 simple queries, I think they get queued. But what happens if 2 batch insert queries intersect at the same time?
    • @Alastor what will happen if some one run single insert along with batch insert? will get the right ID's? for example batch is running and ids from table comes like - 1,2,3,4,single insert here,6. Will it return - 1,2,3,4,6 or 1,2,3,4,5,6 on batch insert?

This topic with respect to MySQL has been previously discussed:

How can I Insert many rows into a MySQL table and return the new IDs?

In CodeIgniter, using $this->db->insert_id() will return only id of the first insert.

If I were doing this, I would open a transaction and do the inserts one at at time, storing the id after each insert.

  • 3
Reply Report

insert_batch() function return only first id of your batch. use insert() function in for loop to get all id's.

public function insert_slike($id, $slike, $folder, $polje, $tabela)
{
    $slike = explode(',', $slike);
    $data = array();
    $ids = array();
    foreach ($slike as $slk) {
        $this->img_upload_resize($slk, $folder);
        $data = array(
             $polje => $id,
             'path' => $slk
             );
        $this->db->insert($tabela, $data);
        $ids[]=$this->db->insert_id();
    }
}
  • 0
Reply Report
      • 1
    • public function insert_slike($id, $slike, $folder, $polje, $tabela) { $slike = explode(',', $slike); $data = array(); $ids = array(); foreach ($slike as $slk) { $this->img_upload_resize($slk, $folder); $data = array( $polje => $id, 'path' => $slk ); $this->db->insert($tabela, $data); $ids[]=$this->db->insert_id(); } }

I agree with Marc. The CI insert_batch is just use foreach to do every insert, so if you use transaction to do these things. You will get the same effect or better effect and also you can get all insert_id after insert.

  • -3
Reply Report
    • CI insert_batch is not the same as as foreach for single insert. insert_batch will use only one query to insert multiple elements.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions