When running extended inserts on a table with an AUTO_INCREMENT field, the value of mysqli_insert_id() will equal the value of the *first* row inserted, not the last, as you might expect.
<?
//mytable has an auto_increment field
$db->query("INSERT INTO mytable (field1,field2,field3) VALUES ('val1','val2','val3'),
('val1','val2','val3'),
('val1','val2','val3')");
echo $db->insert_id; //will echo the id of the FIRST row inserted
?>
mysqli_insert_id,
(PHP 5)
mysqli_insert_id, mysqli->insert_id — 直近のクエリで使用した自動生成の ID を返す
説明
手続き型:
int mysqli_insert_id ( mysqli $link )オブジェクト指向型(プロパティ):
class mysqli {int insert_id
}
mysqli_insert_id() 関数は、AUTO_INCREMENT 属性を 持つカラムがあるテーブル上でのクエリにより生成された ID を返します。 直近のクエリが INSERT あるいは UPDATE ではなかった場合、あるいは 変更されたテーブルに AUTO_INCREMENT 属性を持つカラムがなかった場合は この関数はゼロを返します。
注意: LAST_INSERT_ID() 関数を使用して INSERT あるいは UPDATE ステートメントを 実行すると、mysqli_insert_id() 関数の返す値も 変更されます。
パラメータ
- link
手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。
返り値
直前のクエリで更新された AUTO_INCREMENT フィールドの値を返します。接続での直前のクエリがない場合や クエリが AUTO_INCREMENT の値を更新しなかった場合は ゼロを返します。
注意: もし数値が int の最大値をこえた場合、mysqli_insert_id() は文字列で結果を返します。
例
例 1476. オブジェクト指向型
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
/* テーブルを削除します */
$mysqli->query("DROP TABLE myCity");
/* 接続を閉じます */
$mysqli->close();
?>
例 1477. 手続き型
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($link, "CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($link, $query);
printf ("New Record has id %d.\n", mysqli_insert_id($link));
/* テーブルを削除します */
mysqli_query($link, "DROP TABLE myCity");
/* 接続を閉じます */
mysqli_close($link);
?>
上の例の出力は以下となります。
New Record has id 1.
mysqli_insert_id
Nick Baicoianu
04-May-2007 05:10
04-May-2007 05:10
will at phpfever dot com
21-Apr-2006 10:40
21-Apr-2006 10:40
I have received many statements that the insert_id property has a bug because it "works sometimes". Keep in mind that when using the OOP approach, the actual instantiation of the mysqli class will hold the insert_id.
The following code will return nothing.
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$result->insert_id;
}
?>
This is because the insert_id property doesn't belong to the result, but rather the actual mysqli class. This would work:
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$mysqli->insert_id;
}
?>
matix.bg a gmail
14-Mar-2006 07:56
14-Mar-2006 07:56
Some people are wondering how to get the ids of the rows inserted with loop. Here is it:
<?php
// stmt_init ...
for (;;;) {
// do some things...
$stmt->execute();
$ids[] = $mysqli->insert_id;
}
?>
alan at commondream dot net
04-Nov-2004 04:44
04-Nov-2004 04:44
I was having problems with getting the inserted id, and did a bit of testing. It ended up that if you commit a transaction before getting the last inserted id, it returns 0 every time, but if you get the last inserted id before committing the transaction, you get the correct value.