For some reason I cannot get the LastInsertId
from my PDO
insert. I am receiving nothing back, if I place it inside the execute I get -1 because the insert query hasn't run. However when just after the execute to grab the last ID inserted nothing is returned.
php 5.1.6
PECL pdo = 0.1.0
I have looked at following questions and many other stack exchange questions.
However the difference is nothing is returned compared to 0. No errors are recorded either. See the code below.
Connection
try {
$conn = new PDO("pgsql:host=localhost port= dbname=", "", "");
echo "PDO connection object created";
}
catch(PDOException $e) {
echo $e->getMessage();
}
insert / return the last id
$stmt = $conn ->prepare("INSERT INTO sheet_tbl (site_id, username, additionalvolunteers) VALUES(?,?,?)");
$stmt->bindParam(1,$site_id);
$stmt->bindParam(2,$username1);
$stmt->bindParam(3,$additionalvolunteers);
$site_id = $_POST['site_id'];
$username1 = $user->name;
$additionalvolunteers = $_POST['additionalvolunteers'];
$stmt ->execute();
$newsheetID = $conn->lastInsertId('sheet_id');
echo $newsheetID . "last id";
Assumming sheet_id type is SERIAL you can use
LASTVAL()
TRY (Not tested as I dont have postgresql)