Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting latest inserted auto-incremented record id using fat-free-framework

I need to get the id(AUTO-INCREMENTED) of latest inserted record in a table. I am using fat-free-framework.

I tried to get the latest id by using

$id = mysql_insert_id();

but it gave me this error

Access denied for user 'root'@'localhost' (using password: NO)

I am accessing database using fat-free-framework and not using traditional php functions. Can any one guide me how to accomplish this ?

like image 365
Umair Avatar asked Feb 01 '14 07:02

Umair


3 Answers

Beside kumar_v's answer, F3 will automatically populate $db->_id after a successfull insert.

like image 36
sascha Avatar answered Sep 19 '22 18:09

sascha


Try this code after record inserted

$id = $db->lastInsertId();
like image 108
Kumar V Avatar answered Sep 19 '22 18:09

Kumar V


Note that if you have used the SQL Mapper to create the row in your table, you can just do

$object->id;

Example (using a table containing quotes):

$quote = new DB\SQL\Mapper($db, 'quotes');
if($_POST){
    //overwrite with values just submitted
    $quote->copyFrom('POST');
    $quote->save();
    die("new quote added with id:".$quote->id);
}
like image 39
pixeline Avatar answered Sep 18 '22 18:09

pixeline