简体   繁体   中英

How to get all inserted records ids in codeigniter?

How to get all inserted records ids in codeigniter ?? I am using insert_batch function in for inserting multiple records but codeigniter $this->db->insert_id() function returns only last record id. //But i need all inserted records ids. If any one has idea please help.

//In codeigniter this function returns only last inserted record id.

function insertStudent(){

    $data[] = array('firstname'=>'firstname1','lastname'=>'lastname1');

    $data[] = array('firstname'=>'firstname2','lastname'=>'lastname2');

    $result = $this->db->insert_batch( 'student', $data );

    return $this->db->insert_id();

}

This can be achieved by using first id and count.

  1. Get count of data that you are inserting. (eg 2 in your example)
  2. Get first insert id by $this->db->insert_id() (eg say 30)
  3. Now get last insert recorded id by = first record id - (count - 1) (eg 30 + (2-1) = 31)

Now you can query to get data between id's 30 and 31.

If you absolutely must have the insert_id for each inserted row, the simplest most failproof way would be to loop multiple inserts rather than making one big fat insert.

Looping through multiple inserts will allow you to get all insert_ids. It will also allow you to not loose all the data if one of the rows fails inserting and the database rejects them all because of that.

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('student', $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.

function insertStudent(){

    $data[] = array('firstname'=>'firstname1','lastname'=>'lastname1');

    $data[] = array('firstname'=>'firstname2','lastname'=>'lastname2');

    $result = $this->db->insert_batch( 'student', $data );

    $a= $this->db->get('student');
    $data = $a->result_array();
    echo($data[0]['id']);

}

Hope So this will help you.

My best solution is to update the DB_driver.php.

  • From
public function is_write_type($sql)
{
  return (bool) (preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
}
  • To:
public function is_write_type($sql)
{
  return (bool) (preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql) && !preg_match('/ RETURNING /i', $sql));
}

The really safest way is:

  1. Add a column code in your table.
  2. Create a code before batch inserting [id session user]-[current time stamp] should be enough.
  3. Batch insert all elements with that code into code column
  4. Query a Select searching for that code, you will have all elements inserted at batch insert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM