How can I get the insert_id of the last record using mysqli prepare statement, for example following?
$stmt = $this->mysqli->prepare("INSERT INTO test (name) values (?)")
$stmt->bind_param('s', $name);
$stmt->execute();
A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").
Description ¶In the case of a multiple-row INSERT statement, it returns the first automatically generated value that was successfully inserted. Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() MySQL function will also modify the value returned by mysqli_insert_id().
" $stmt " obviously (I think) stands for "statement". As a variable name it's arbitrary, you can name that variable anything you want. $stmt is just rather idiomatic. A prepared statement as such is a database feature.
Codeigniter provide method insert_id() to get last inserted id. insert_id() is function of "db" library. db library provides several function for connecting database task. insert_id() will return id of last inserted record. So here i give controller method so you can understand how it is working.
$new_id = $this->mysqli->insert_id;
(after $stmt->execute()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With