The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Inserted objects ID - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Support Center (https://userspice.com/forums/forumdisplay.php?fid=23)
+--- Forum: UserSpice 4.3 and Below (https://userspice.com/forums/forumdisplay.php?fid=26)
+--- Thread: Inserted objects ID (/showthread.php?tid=174)



Inserted objects ID - lefox - 06-28-2016

Is there a way to get the ID of last inserted object?


Inserted objects ID - brian - 06-28-2016

In the DB.php class file, there is the following function you can use on the PDO object.

public function lastId(){
return $this->_lastId;
}



Inserted objects ID - mudmin - 06-28-2016

I added that function because that issue always baffled me, and then I never actually used it in writing userspice.

Let's say you want to add a new user named Bob to your database whenever someone visited your homepage (which wouldn't be advised).

Code:
<?php
Code:
$fields=array('username'=>"bob");
Code:
$db->insert('users',$fields);
Code:
$lastId = $db->lastId();
Code:
echo $lastId;
Code:
?>

Loading this page would echo out the latest "Bob" user id.