3

I am using ADODB to connect to my database. After I submit the query I want to get the ID of the last inserted row (from the query just inserted).

ADODB has an Insert_ID() function that should retrieve this is but it is not...
db->Insert_ID()
is not working, neither is
db->Insert_ID($table, $key)

They both just return empty values. I doubled checked my table and the insert statement is indeed working, a new row is being put in, and the key is auto_increment. Am I using the Insert_ID wrong or is there a better way to retrieve the key of the last row inserted?

Thanks

Edit: Adding code

    $result = \PSU::db->Execute( $updateSQL, $values_array );
    $id = \PSU::db->Insert_ID();
    // $id = \PSU::db->Insert_ID( $table, $key );

\PSU::db is our ADODB implementation class, taking care of things like connecting, disconnecting, etc.

kreynolds
  • 426
  • 4
  • 15
  • show us your exact code that you're using. – Jan Hančič Jan 23 '12 at 14:17
  • try this one $db->_connectionID->insert_id; after your insert – Sergey Benner Jan 23 '12 at 14:19
  • 2
    [`Insert_ID()`](http://phplens.com/lens/adodb/docs-adodb.htm#inserted_id) has no parameters and returns the last insert id only if the database support auto-increment IDs. If it's not supported it returns `FALSE`. – hakre Jan 23 '12 at 14:23
  • Like I said in the post, the table does support auto increment – kreynolds Jan 23 '12 at 14:24
  • Regarding your edit: Please show how you assign an object to a class constant. I wonder how you magically perform that. – hakre Jan 23 '12 at 14:26
  • PSU is our overhead that contains most of our custom libraries. The more I ask around here the more I realize unless you have been here for 5+ years you are not exactly sure why it works, it just does :-/ Basically the PSU::db class when used will make an automatic DB connection to the database you specify (IE \PSU::db('support') then you can use any AODB command to do what you need to. It will return success/failure on insert/update and will return an array for selects. I lft out the table name b/c I do not believe it to be part of the problem, it is inserting, fetching, updating just fine. – kreynolds Jan 23 '12 at 14:33
  • Got it, our ADODB version needed to be updated. Thanks for the help. – kreynolds Jan 23 '12 at 14:52

2 Answers2

0

Try this:

$result = \PSU::db->Execute( $updateSQL, $values_array );

return \PSU::db->_connectionID->insert_id ;

_connectionID must be fixed as is.

Hope it helps!

MrHiggs
  • 7
  • 3
0

Is it possible that you are disconnecting and reconnecting to mysql between the query and the insert_id()?

TrippyD
  • 735
  • 5
  • 9