How to get ID of the last record in the database?

Good day, tell me on this question, there is such script
$products = 'INSERT INTO product(sku, supplier, quantity, price, status) VALUES';
 foreach($xml->products as $product) {
 $products .= sprintf('("%s, %s, %s, %s, %s")', $product->product_code, 'supplier', '$product->stock, $product->price, '1');
}
$db->query($products);

But the problem is that I need some more data to bring in another table, but for that I need to id poldini goods. I tried to do so
$ids[] = $db->insert_id();
But I receive an error. Where am I wrong?
Thanks in advance for any advice
PS for database connection
function connectDB()
{
 $dbHostNameWithPort = DB_HOSTNAME;
 $dbUserName = DB_USERNAME; 
 $dbPassword = DB_PASSWORD;
 $dbDB = DB_DATABASE;
 $dbLink = mysqli_connect( $dbHostNameWithPort, $dbUserName, $dbPassword ); 
 $dbLink->query("SET character_set_results=utf8");
 $dbLink->query("set names 'utf8'");
 $dbLink->query( "USE ".$dbDB .";" );
 return $dbLink;
}
April 7th 20 at 11:01
1 answer
April 7th 20 at 11:03
Solution
No "last record in database" mysql does not exist.
You can get only one ID of the last inserted row. one.
So if you need ajtishniki, you need to insert one.

All this horror and shamanic dances with a tambourine it is necessary to rewrite
to connect with dB
function connectDB()
{
 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 $dbLink = mysqli_connect( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 
$dbLink->set_charset("utf8mb4");
 return $dbLink;
}
and the request itself
$sql = "INSERT INTO product(sku, supplier, quantity, price, status) 
 VALUES (?, 'supplier', ?, ?, 1)";
$stmt = $db->prepare($sql);
$ids = [];
foreach($xml->products as $product) {
 $stmt->bind_param("sss", ...[$product->product_code, $product->stock, $product->price]);
$stmt->execute();
 $ids[] = $db->insert_id;
}
@Gloria_Denesik Thanks for the advice, I'll try - dashawn.Nitzsche12 commented on April 7th 20 at 11:06
@Gloria_Denesik th truth?
psql=> create table t (id serial not null, x varchar not null);
psql=> insert into t (x) values ('a'),('b'),('c') returning id;
 id 
----
1
2
3
(3 rows)
- Savanah_McCullough85 commented on April 7th 20 at 11:09
WELL, I get the same error Call to undefined method mysqli::insert_id() - dashawn.Nitzsche12 commented on April 7th 20 at 11:12
Without parentheses need is a property and not a method

Retarded interface, proceduralism is a function and an object variable.
You could in the manual to look at - Gloria_Denesik commented on April 7th 20 at 11:15
Thank you all for the advice, figured out - dashawn.Nitzsche12 commented on April 7th 20 at 11:18

Find more questions by tags PHP