Getting the last inserted auto-incrementing record identifier using an oil-free structure

I need to get the identifier (AUTO-INCREMENTED) of the last inserted record in the table. I use oil-free frames.

I tried to get the last id using

$id = mysql_insert_id();

but he gave me this error

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

I access the database using an oil-free structure and do not use traditional php functions. Can anyone advise me how to do this?

+4
source share
4 answers

Try using this code after writing

$id = $db->lastInsertId();
+10

kumar_v, F3 $db->_id .

+1

Please note: if you used SQL Mapper to create a row in your table, you can simply do

$object->id;

Example (using a table containing quotation marks):

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

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

0
source

Source: https://habr.com/ru/post/1524816/


All Articles