简体   繁体   中英

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 ?

插入记录后尝试此代码

$id = $db->lastInsertId();

除了kumar_v的答案,F3会在成功插入后自动填充$db->_id

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);
}
$quote = new DB\SQL\Mapper($db, 'quotes');
$quote->get('_id');

which '_id' is the id auto increment id field of your table, replace '_id' with yours you can read the docs : https://fatfreeframework.com/3.6/sql-mapper#get

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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