$query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )";
$stmt = $pdo->prepare($query);
$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);
$data = $stmt->execute($params);
// here i would like get current inserted ID. Is possible?
$id = $data->id ???? ;
我该怎么做?
$query = "INSERT INTO news VALUES (NULL, :param1 , :param2 )";
$stmt = $pdo->prepare($query);
$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);
$data = $stmt->execute($params);
所以您可以这样做以获得最后插入的ID
$last_id = $pdo->lastInsertId();
使用:
$last_insert_id = $pdo->lastInsertId();
您可以使用PDO::lastInsertId
$last_insert_id = $pdo->lastInsertId();