Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get insert_id using mysqli prepare statements

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();
like image 973
Ajay Avatar asked Apr 06 '11 23:04

Ajay


People also ask

What is MySQLi prepared statement?

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 "?").

What is Insert_id?

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().

What is $STMT in PHP MySQLi?

" $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.

What is Insert_id in codeigniter?

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.


1 Answers

$new_id = $this->mysqli->insert_id; (after $stmt->execute())

like image 110
a1ex07 Avatar answered Oct 17 '22 10:10

a1ex07