Questions tagged [insert-id]

47 questions
430
votes
15 answers

How to get the insert ID in JDBC?

I want to INSERT a record in a database (which is Microsoft SQL Server in my case) using JDBC in Java. At the same time, I want to obtain the insert ID. How can I achieve this using JDBC API?
Satya
  • 8,146
  • 9
  • 38
  • 43
150
votes
7 answers

MySQL ON DUPLICATE KEY - last insert id?

I have the following query: INSERT INTO table (a) VALUES (0) ON DUPLICATE KEY UPDATE a=1 I want the ID of either the insert or the update. Usually I run a second query in order to get this as I believe insert_id() only returns the 'inserted' ID…
thekevinscott
  • 5,263
  • 10
  • 44
  • 57
24
votes
4 answers

mysql_insert_id alternative for postgresql

is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks are solving the problem partially by finding the current value of the sequence used in the ID. However, there are times that the primary key is not a…
Jeffrey04
  • 6,138
  • 12
  • 45
  • 68
11
votes
6 answers

Insert date and time into Mysql

I am trying to insert date and time into mysql datetime field. When a user select a date and time, it will generate two POST variables. I have searched internet but still not sure how to do it. My code. //date value is 05/25/2010 //time value is…
FlyingCat
  • 14,036
  • 36
  • 119
  • 198
8
votes
5 answers

MySQL: use the id of the row being inserted in the insert statement itself

I'm trying to some something like that: INSERT INTO dir_pictures SET filename=CONCAT(picture_id,'-test'); picture_id is my primary key, auto-increment. Basically I'm trying to put the id of this insert statement, in the statement itself. I'm sure…
Nathan H
  • 48,033
  • 60
  • 165
  • 247
6
votes
3 answers

Retrieve Core Data Entities in Order of Insertion

Is there an internal ID variable or timestamp I can use in a NSSortDescriptor to retrieve Core Data entities in the order they were inserted? I would rather not have to create such properties if there is a cleaner way, though will obviously do so if…
Andy Shea
  • 999
  • 9
  • 17
5
votes
4 answers

zend framework get last insert id of multi row insert using execute

How would I get the last inserted ID using a multirow insert? Here is my code: $sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example $stmt = $contactsTable->getAdapter()->prepare($sql); $stmt->execute();…
EricP
  • 1,459
  • 6
  • 33
  • 55
5
votes
1 answer

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

I been programming 3~4 years ago, and I remember that in some 2005 php security book about the safety of using mysql_insert_id to retrieve the last_inser_id. In the book says that if there is several request to the server at the same time…
Jose Faro
  • 356
  • 1
  • 3
  • 13
4
votes
4 answers

MySQL current_insert_id()? (Not last_insert_id())

I am inserting a row with a char column for a hash based on (among other things) the row's auto id. I know I can insert it, fetch the insert_id, calculate the hash, and update it. Does anyone know of a way to do this in a single query? You would…
Eli
  • 97,462
  • 20
  • 76
  • 81
4
votes
1 answer

PHP, MySql, mysqli insert_id Returns 0 Always

System / development specs; Windows 7 Ultimate x64 SP1 + all updates PHP Version 5.5.15 Non-Thread Safe (x64 Experimental) MySQL Server 5.6 (x64) Php mysqli I'm executing a stored procedure that will insert a user name and password into a table…
njc
  • 128
  • 2
  • 8
4
votes
2 answers

PHP – retrieving auto-incremented values from extended inserts

I'm currently using $mysqli->insert_id to retrieve auto-allocated primary keys from a MySQL table when new rows are inserted. This works fine on single inserts. It's a simple case of $result = $mysqli->query($query); $NewPrimaryKey =…
Simon Roberts
  • 617
  • 1
  • 7
  • 14
3
votes
3 answers

Postgresql and PHP: is the currval a efficent way to retrieve the last row inserted id, in a multiuser application?

Im wondering if the way i use to retrieve the id of the last row inserted in a postgresql table is efficent.. It works, obviously, but referencing on the serial sequence currval value could be problematic when i have many users adding rows in the…
Strae
  • 18,807
  • 29
  • 92
  • 131
3
votes
2 answers

mysqli->insert_id on update (PHP)

Does it work for anyone? :P I can properly get insert_id while inserting, but not on update. Of course contactsId column is AUTO_INCREMENT. Whole code:
Line
  • 1,529
  • 3
  • 18
  • 42
3
votes
3 answers

how do i handle concurrent inserts in mysql table and fetch the correct insert id

I am using adodb for PHP library. For fetching the id at which the record is inserted I use this function "$db->Insert_ID()" I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the…
Gaurav Sharma
  • 2,830
  • 1
  • 37
  • 54
3
votes
2 answers

Insert_id is null when used directly in next prepared statement

Finally getting around to learning prepared statements. I'm getting a maddening error when trying to run a pair of simple inserts: $p_stmt = $mysqli->prepare("INSERT INTO ww_pages (page_key) VALUES (?)"); $p_stmt->bind_param('s',…
CodeMoose
  • 2,964
  • 4
  • 31
  • 56
1
2 3 4