Insert Returning last ID with PDO

Tom

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/>';
Skot

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SELECT LAST_INSERT_ID() returning zero

From Dev

PDO last insert id returns null or no method found

From Dev

PDO not being able to return the last_insert_id

From Dev

Getting last insert id 0 in magento with Zend_Db_Adapter_Pdo_Mysql in magento

From Dev

Is it mandatory to insert a new record just before calling lastInsertId() to get the last inserted ID in PDO?

From Dev

PHP PDO insert not returning expect from operation

From Dev

Getting ID of last insert

From Dev

SQL, select last insert ID by LAST_INSERT_ID()

From Dev

Get last insert id of Postgresql

From Dev

Mysqli last insert id not work

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

How to get the insert ID after insert using the PDO transactino in php

From Dev

mysql_insert_id() is returning 0

From Dev

INSERT INTO ... FROM SELECT ... RETURNING id mappings

From Dev

PostgreSql INSERT FROM SELECT RETURNING ID

From Dev

Returning AutoInc ID after Insert in Slick 2.0

From Dev

Cant use id in function after insert returning into

From Dev

Codeigniter insert_id() function retrieve correctly the last insert id?

From Dev

how to select last_insert_id?

From Dev

Get last insert id return null

From Dev

How to get last insert id with Phalcon?

From Dev

How efficient is Last_insert_id?

From Dev

how to get the last insert ID in linq

From Dev

MYSQL last_insert_id() and concurrency

From Dev

Return the id of the last MySQL insert in PHP

From Dev

Implement a method that generates the last insert id

From Dev

Get ID from last insert into table

From Dev

Phalcon last insert id in aftercreate function

From Dev

Nodjs sequalize how to get last insert id

Related Related

HotTag

Archive