0
$db->query("INSERT into `users` (username, password, email)
VALUES ('$username', '$hashpassword', '$email')");
$userid = mysqli_insert_id($db->link);
$db->query("INSERT into `users logins` (userid, token, ip)
VALUES ('$userid', '$token', '$ip')");
以上是舊代碼。以下是我的新產品:將兩個插入查詢與事務合併。但是我需要抓住第一個插入行ID
$db->query("BEGIN TRANSACTION;
INSERT into `users` (username, password, email)
VALUES ('$username', '$hashpassword', '$email');
[here I need to get the row ID of the above insert]
INSERT into `users logins` (userid, token, ip)
VALUES ('$userid', '$token', '$ip')
COMMIT;");
如何獲取第一個插入行ID並將其用於第二次插入?
真棒。但是我忘了提到我在查詢後面設置了一個cookie:setcookie(「id」,$ userid,time()+ 941920000,「/」)。其中$ userid將是行ID。我能在這裏做什麼嗎? – ditto
看看'mysql_insert_id()'[link here](http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html) –
我最初使用的是mysqli_insert_id(),但是,我需要拉第一個插入的行ID。事務後使用它將拉第二個插入ID。 – ditto