简体   繁体   English

如何在codeigniter中获取所有插入的记录ID?

[英]How to get all inserted records ids in codeigniter?

How to get all inserted records ids in codeigniter ?? 如何在codeigniter中获取所有插入的记录ID? I am using insert_batch function in for inserting multiple records but codeigniter $this->db->insert_id() function returns only last record id. 我在使用insert_batch函数插入多个记录,但是codeigniter $ this-> db-> insert_id()函数仅返回最后一个记录ID。 //But i need all inserted records ids. //但是我需要所有插入的记录ID。 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. 这可以通过使用第一个id和count来实现。

  1. Get count of data that you are inserting. 获取要插入的数据计数。 (eg 2 in your example) (例如,您的示例中为2)
  2. Get first insert id by $this->db->insert_id() (eg say 30) 通过$this->db->insert_id()获取第一个插入ID(例如说30)
  3. Now get last insert recorded id by = first record id - (count - 1) (eg 30 + (2-1) = 31) 现在获取最后插入的记录ID等于= first record id - (count - 1) (例如30 +(2-1)= 31)

Now you can query to get data between id's 30 and 31. 现在,您可以查询以获取ID在30到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. 如果绝对必须为每个插入的行都具有insert_id,则最简单的最可靠的方法是循环多个插入,而不是制作一个大的胖插入。

Looping through multiple inserts will allow you to get all insert_ids. 循环遍历多个插入将允许您获取所有insert_id。 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. 由于您的记录具有结果ID,因此我将假设它们也自动递增。

If this is the case, you do this and still use insert_batch. 如果是这种情况,请执行此操作,并且仍然使用insert_batch。

Here is what you do: 这是您的工作:

  1. You take a count of the items you are inserting: 您对要插入的项目进行计数:

    $count = count($data); $ count = count($ data);

  2. Run your batch insert: 运行您的批处理插入:

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

  3. Get the first inserted ID of your batch: 获取您的批次的第一个插入的ID:

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

  4. Add the count (minus 1) to your insert ID to get the last records ID. 将计数(减1)添加到插入ID中以获取最后的记录ID。

    $last_id = $first_id + ($count-1); $ 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. 现在,您有了插入记录的第一个和最后一个ID,并且通过扩展,还可以找到其间的所有其他内容。

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. 我最好的解决方案是更新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. 在表中添加列code
  2. Create a code before batch inserting [id session user]-[current time stamp] should be enough. 批量插入[id session user]-[current time stamp]前,请创建代码。
  3. Batch insert all elements with that code into code column 将具有该代码的所有元素批量插入code
  4. Query a Select searching for that code, you will have all elements inserted at batch insert 查询一个Select搜索该代码,您将在批量插入时插入所有元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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