doubaoxue5788 2016-11-12 11:24
浏览 98

获取最后插入的id返回0

I'm trying to get the id of the last inserted row. 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. The id of the table is auto increment and primary. What am I doing wrong? Thank you for your help :)

  • 写回答

1条回答 默认 最新

  • doulou0882 2016-11-12 11:44
    关注

    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]
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度