获取最后插入的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 :)

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]