• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active record last insert ID

#5
[eluser]Unknown[/eluser]
@meovino

$this->db->insert_id() is a simple wrapper to the PHP MySQL classes mysql_insert_id() function. The only difference is that is suppresses any errors that may come from the call by adding the @ in front of it, and it passes the connection id (which is optional). Here is the code I am referring to from the mysql_driver.php CI file:

Code:
/**
* Insert ID
*
* @access public
* @return integer
*/
function insert_id()
{
  return @mysql_insert_id($this->conn_id);
}

However I think its important to note the difference between the PHP function that this wraps and the direct MySQL function of retrieving the last auto-incremented value for the session. An important difference to note is the following:

In the instance when multiple rows are created with a single insert statement LAST_INSERT_ID() (the direct MySQL function) will return the first inserted rows ID, whereas MYSQL_INSERT_ID() (the PHP function the CI wraps up with $this->db->insert_id()) returns the ID from the last row inserted.

See the following documentation references:
http://dev.mysql.com/doc/refman/5.0/en/m...rt-id.html
Quote:In the case of a multiple-row INSERT statement, mysql_insert_id() returns the first automatically generated AUTO_INCREMENT value; if no such value is generated, it returns the last explicit value inserted into the AUTO_INCREMENT column.

http://us.php.net/mysql_insert_id
Quote:Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.

Thanks,
Rob


Messages In This Thread
Active record last insert ID - by El Forum - 08-01-2008, 01:05 PM
Active record last insert ID - by El Forum - 08-01-2008, 02:16 PM
Active record last insert ID - by El Forum - 08-01-2008, 02:23 PM
Active record last insert ID - by El Forum - 08-03-2008, 08:50 PM
Active record last insert ID - by El Forum - 08-03-2008, 10:58 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.