PDO - get current inserted ID

后端 未结 3 1700
礼貌的吻别
礼貌的吻别 2021-01-12 08:45
$query = \"INSERT INTO news VALUES (NULL, :param1 , :param2  )\";
$stmt = $pdo->prepare($query);

$params = array(
    \"param1\" => $p[\'title\'],
    \"param         


        
相关标签:
3条回答
  • 2021-01-12 09:24

    You could use PDO::lastInsertId

    $last_insert_id = $pdo->lastInsertId();
    
    0 讨论(0)
  • 2021-01-12 09:34

    Use :

    $last_insert_id = $pdo->lastInsertId();
    
    0 讨论(0)
  • 2021-01-12 09:42
    $query = "INSERT INTO news VALUES (NULL, :param1 , :param2  )";
    $stmt = $pdo->prepare($query);
    
    $params = array(
        "param1" => $p['title'],
        "param2" => $p['body'],
    );
    
    $data = $stmt->execute($params);
    

    so you can do like this to get last inserted Id

    $last_id = $pdo->lastInsertId();
    
    0 讨论(0)
提交回复
热议问题