Questions tagged [lastinsertid]

135 questions
2
votes
1 answer

Doctrine 2 - How to get the ID of the last inserted id in PostPersist?

Title explains it all. I have a lifecyclecallback function in entity. I want to get last inserted id from PostPersist event not from the entity. AS an example I dont want to do $newSeating = new…
ufucuk
  • 475
  • 9
  • 20
2
votes
1 answer

Why does PDO::lastInsertId return 0?

This is my code. // insert reward into wallet $sql = " INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id); "; $sth = self::link()->prepare($sql); // primary…
The Surrican
  • 26,829
  • 23
  • 111
  • 159
2
votes
2 answers

last_insert_id() vs SELECT Max(ID)

Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id(), where they run as 2 separate queries? I'm concerned that before the last_insert_id() can run, another insert will take place.
Yisroel
  • 8,144
  • 4
  • 24
  • 26
2
votes
2 answers

Get last Inserted 'Id' while doing multiple entry to mysql in codeigniter

In my form, there are values to be inserted to multiple tables. After inserting to the first table, i have to insert the other values, along with the 'id' of the entry to first table as reference. What is the best way to do this? Is there any…
esafwan
  • 14,622
  • 30
  • 99
  • 154
2
votes
2 answers

pdo lastInsertId returns zero(0)

All queries execute successfully, when I check table in MySQL row inserted successfully without any error, but lastInsertId() returns 0. why? My code: // queries executes successfully, but lastInsetId() returns 0 // the menus table has `id` column…
Arash Mousavi
  • 2,023
  • 3
  • 19
  • 39
2
votes
3 answers

PDO LastInsertId returning nothing

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…
Tom
  • 654
  • 2
  • 9
  • 24
2
votes
1 answer

How can i return the id of the last updated record in MySQL?

I am using PDO to connect to MySQL. When even I add new record to the database and want to know the id of the last record added then I call the lastinsertid() built in PDO. However, this time I am trying to return the last updated record. According…
Jaylen
  • 33,621
  • 35
  • 108
  • 193
2
votes
1 answer

How to get last insert id when using sub query in Kohana

How do I return the correct $insert_id when using a sub query in Kohana? I'm using the query method to return $insert_id and $affected_rows. It returns the correct value for $affected_rows but returns '1' for $insert_id which is incorrect. Query…
sensoria
  • 23
  • 4
1
vote
1 answer

last_insert_rowid in sqlite - two tables

I got two tables, one of which is a helper of the other. The table "reviews" has a column which is an integer referencing to the id of the film of that review in the table "films". Is there a way to, after inserting in the table "films" a new film,…
1
vote
1 answer

MySQL PDO LastInsertID after insert with "before" trigger returns wrong value

I have a customers table with ID (auto_inc), name, ... and customer_code fields. This table has also a "before" trigger, the select one customer code from another table, and set the customer_code field before insert a new record in this table. Works…
1
vote
1 answer

Sybase JDBC get generated keys

In Postgres, I can write INSERT .. RETURNING * To retrieve all values that had been generated during the insert. In Oracle, HSQLDB, I can use String[] columnNames = ... PreparedStatement stmt = connection.prepareStatement(sql, columnNames); //…
Lukas Eder
  • 181,694
  • 112
  • 597
  • 1,319
1
vote
1 answer

Not able to get last inserted ID from MySQL

I'm inserting multiple rows from one table to another, based on ID. For this project I'm using PDO for all DB queries. This is the code / function I'm using: protected function importData($data) { $i = 0; $this->db->beginTransaction(); …
Steven
  • 18,168
  • 44
  • 141
  • 240
1
vote
3 answers

Rails 3 - how to find last inserted ID from mysql table

in my controller I have following sequence of commands: SAVE DATA INTO FIRST TABLE _get ID of inserted item into table from first step_ SAVE DATA INTO SECOND TABLE WITH ID FROM FIRST COMMAND if FIRST.save && SECOND.save do something And I am…
user1946705
  • 2,746
  • 13
  • 39
  • 56
1
vote
1 answer

Last inserted row id in PostgreSQL

I am using postgresql database. And previous tables which are created by my seniors and they are not used sequence concept, they added data manually. Now i need to insert data with my java application. for that i need last inserted ID. I have no…
vissu
  • 1,721
  • 7
  • 35
  • 50
1
vote
3 answers

SELECT LAST_INSERT_ID() *updated

I'm looking to use SELECT LAST_INSERT_ID() Am using a form to have a user input values. With the first insert I need to get the last inserted id for the next insert... I have not figured out how to get the last selected id and then pass it into my…
Droid646197
  • 65
  • 2
  • 2
  • 8
1 2 3
8 9