[英]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. 我需要在表中获取最新插入记录的id(AUTO-INCREMENTED)。 I am using fat-free-framework. 我正在使用无脂肪框架。
I tried to get the latest id by using 我试图通过使用获得最新的ID
$id = mysql_insert_id();
but it gave me this error 但它给了我这个错误
Access denied for user 'root'@'localhost' (using password: NO) 用户'root'@'localhost'拒绝访问(使用密码:否)
I am accessing database using fat-free-framework and not using traditional php functions. 我使用无胖框架访问数据库,而不是使用传统的PHP函数。 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 请注意,如果您已使用SQL Mapper在表中创建行,则可以这样做
$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 哪个'_id'是你的表的id自动增量id字段,用你的'_id'替换你可以阅读文档: https : //fatfreeframework.com/3.6/sql-mapper#get
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.