I am trying to get the last record from the database using lastInsertId() but it keeps retuning 0. I don't understand why this problem occurs. Here is my code. Thanks
$query = "SELECT url FROM links WHERE code = :code";
$get = $db->prepare($query);
$get->execute(array(
":code" => $code
));
if($get->rowCount()) {
$url = $get->fetch(PDO::FETCH_OBJ)->url;
$status = substr(get_headers($url)[0],9,3);
if(intval($status) == 301) {
$last = $db->lastInsertId();
$query = "SELECT url FROM links WHERE id = :id";
$send = $db->prepare($query);
$send->execute(
":id" => $last
);
$lastUrl = $send->fetch(PDO::FETCH_OBJ)->url;
header("Location:{$lastUrl}");
}
else {
header("Location:{$url}");
}
die();
}