How to get the last ID inserted with PDO in PHP

1 Answer

0 votes
try 
{
    $con = new PDO("mysql:host=".$db_host.";dbname=...");
    
    $inserted = $con->prepare("INSERT INTO table_name(url, title)
    VALUES(:url, :title");

    $inserted->execute(array(
        "url" => $url,
        "title" => $title
    ));

    // PDO last inserted ID
    $webpage_id = $con->lastInsertId();
}
catch (PDOException $e) 
{
    echo $e->getFile() . " : " . $e->getLine() . " : " . $e->getMessage();
}

/*
run: 


*/

 



answered Jun 30, 2016 by avibootz
...