I am trying to get the last/current ID submitted in an Insert, I have tried lastInsertId
however that didn't work. Alternatively I have used returning
on the end of my insert. However that was using pg_sql. How would I use the returning line, with PDO? I am stuck with the logic of getting the value displayed using PDO in the second option.
php 5.1.6
See below
Doesn't Work
$stmt ->execute();
$newsheetID = $conn->lastInsertId('sheet_id');
echo $newsheetID . "last id";
Works But is pg_sql, I would like to get this working for PDO
$sql = "INSERT INTO sheet_tbl (site_id, username, additionalvolunteers) VALUES ('$_POST[site_id]', '$username','$_POST[additionalvolunteers]') returning sheet_id";
echo $sql;
$result = pg_query($sql);
while ($row = pg_fetch_row($result)) {
$sheet_id_post = $row[0];
echo $sheet_id_post . '<br/>';
Your looking for this, if you cannot get lastInsertId to work, this will do the job, a couple of extra lines tho:
foreach ($stmt as $row)
{
$sheet_id_post = $row[0];
echo $sheet_id_post;
}
You can always msg me. Or head to php website look for a similar loop and alter it, my answer is very similar to yours and when I first did this I just went to php.net :-)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments