简体   繁体   English

获取最后插入的ID返回0

[英]Get last inserted id returns 0

I'm trying to get the id of the last inserted row. 我正在尝试获取最后插入的行的ID。 This is my code: 这是我的代码:

     if($typ == "baby"){
        $query ="INSERT INTO duell (user_id, type, namensliste)
        VALUES ($id, 1, $duell);";
        $result = get_result($query);
          $duell_id =   $pdo->lastInsertId();
    } else if($typ == "hund"){
        $query ="INSERT INTO duell (user_id, type, namensliste)
        VALUES ($id, 2, $duell);";
       $result = get_result($query);
        $duell_id =   $pdo->lastInsertId();
    }

It won't work with lastInsertId() or mysql_insert_id() as it always returns 0 even tough I execute it directly after the Insert and the query works properly. 它不能与lastInsertId()或mysql_insert_id()一起使用,因为它总是返回0,即使在插入和查询正常后我直接执行它也很难。 The id of the table is auto increment and primary. 该表的ID为自动增量和主要。 What am I doing wrong? 我究竟做错了什么? Thank you for your help :) 谢谢您的帮助 :)

in your case if it does not work! 在您的情况下,如果它不起作用! you can use this: 您可以使用此:

$duell_id =   get_result("select id from duell order by id DESC limit 1 ")[0]

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

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