简体   繁体   English

如何从mysql获取最近插入的自动增量ID

[英]How to get recently inserted auto-increment id from mysql

I have a MySQL table, named war_engagement which has an auto-increment id (named w_e_id ) as primary key. 我有一个名为war_engagement的MySQL表,该表具有一个自动增量ID(名为w_e_id )作为主键。 I am inserting records like this 我正在插入这样的记录

//insertion in first table
$query = "INSERT INTO war_engagement (
                `attacker_user_id` ,
                `attacker_city_id` ,
                `attacker_mode` ,
                `result`
                )
                VALUES (
                '$_SESSION[user_id]',
                '$cur_city_id',
                'default',
                ''
                )";
$rs = mysql_query($query) or die(mysql_error()." in query $query");

And if above query executes successfully then I want to insert this recently inserted auto-increment id (that is w_e_id ) as foreign key into another table named war_engagement_troops 如果上面的查询成功执行,那么我想将此最近插入的自动增量ID(即w_e_id )作为外键插入另一个名为war_engagement_troops表中

//if first record inserted successfully then insert second table
if($rs)
{
    echo "'".$sending_troop."' ,";
        $query = "INSERT INTO war_engagement_troops (
                    `w_e_id` ,
                    `quantity` ,
                    `expired`
                    )
                    VALUES (
                    '$recently_inserted_w_e_id',
                    '$quantity',
                    '$expired'
                    )";
    $rs = mysql_query($query) or die(mysql_error()." in query $query");
    if(!$rs) echo $rs;
}

I want to get w_e_id (which is auto-increment) to assign it to $recently_inserted_w_e_id . 我想获取w_e_id (这是自动递增的),将其分配给$recently_inserted_w_e_id

(I also used MAX(w_e_id) to retrieve maximum ID but it is not looking ideal way and can be mixed up in multiple users insertions). (我也使用MAX(w_e_id)来检索最大ID,但它并不是理想的方式,可以在多个用户插入中混合使用)。

Please give me some ideal solution to get recently inserted id/record. 请给我一些理想的解决方案,以获取最近插入的ID /记录。

您可以使用mysql_insert_id()函数获取最后插入的ID,然后可以将其用作另一个表的前瞻键

You are looking for mysql_insert_id() function. 您正在寻找mysql_insert_id()函数。 But I strongly recommend you to use PDO 但我强烈建议您使用PDO

If you don't want to use MAX(w_e_id), Then after the INSERT statement you can use 如果您不想使用MAX(w_e_id),则可以在INSERT语句之后使用

SELECT LAST_INSERT_ID();

We can make use of mysql_insert_id() ; 我们可以使用mysql_insert_id() function as well 以及功能

It will look something like this 它看起来像这样

$query = "INSERT INTO war_engagement ( `w_e_id` , ...)  VALUES ( ...);
//if first record inserted successfully then insert second table
if($rs)
{
    $query = "INSERT INTO war_engagement_troops ( `id` ,  `w_e_id` , ...)  VALUES (  '', LAST_INSERT_ID(), ....)";
                   }

You can use mysqli_insert_id() function or the mysqli::$insert_id property if you use the object-oriented style 如果使用面向对象的样式,则可以使用mysqli_insert_id()函数或mysqli :: $ insert_id属性。

http://php.net/manual/en/mysqli.insert-id.php http://php.net/manual/en/mysqli.insert-id.php

If using PDO, use the method PDO::lastInsertId() 如果使用PDO,则使用方法PDO :: lastInsertId()

http://php.net/manual/en/pdo.lastinsertid.php http://php.net/manual/en/pdo.lastinsertid.php

$recently_inserted_w_e_id = mysql_insert_id();

SELECT TOP 1 w_e_id从war_engagement顺序由w_e_id desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM