lastinsertid

Alternative to "PDO::lastInsertId" / "mysql_insert_id"

I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created. $DB->exec("INSERT INTO example (column1) VALUES ('test')"); // Usually returns your newly created ID. // However w...

Last inserted record ID problems using typed DataSet and DataGridView

Hi. I'm using following for a simple database application: SQL Server Compact Edition .sdf file as database, with int primary key IDs. Typed DataSet and BindingSource as data access layer DataGridView for displaying data. My problem is, I'm having trouble with the last inserted record/row ID. When I add a row to datagridview, using A...

What could cause duplicate ids on a auto increment primary key field (mysql)?

RESOLVED From the developer: the problem was that a previous version of the code was still writing to the table which used manual ids instead of the auto increment. Note to self: always check for other possible locations where the table is written to. We are getting duplicate keys in a table. They are not inserted at the same time (6 h...

retrieve last updated column in mysql

Hi, I have a MySQL query that goes as follows (using Zend_Db): $sql = $handle->quoteInto("UPDATE board SET rank=rank+1 WHERE post_id=?", $postid); $handle->query($sql); (Rank isn't an auto-incrementing PK). I would like to now retrieve the value of rank without preforming another query. I've tried $handle->lastInsertId(); but it doe...

Universal SQL construct to retrieve the last row inserted

What would be the correct universal SQL construct to get the last row inserted (or it's primary key). The ID might be autogenerated by a sequence but I do not want to deal with the sequence at all! I need to get the ID by querying the table. Alternatively, INSERT might be somehow extended to return the ID. Assume I am always inserting a ...

Transaction with two sql insert

I have two sql insert to do (say for examples in tables A and B), they are in a transaction because I want the database to remain consistent, that is, a tuple in A must have references in B. In the second insert I need the id that comes from the first, but I don't get this id until I make a commit on the transaction. So I'm stuck. I do...

How to get the value of id(primary key) in previous operation in MySQL

I am using MySQL. I need to insert one row into a table first, then I need to get the id of the inserted row. The code looks somewhat like the following: insert into mytable (column2, column3, column4) values('value2','value3','value4')or die(mysql_error()); Column1 is the primary key and it is auto-increment. So how to ge...

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 correct inserted id for each record inserted ? The reason I am asking this is because I use t...

With Kohana 3, how can I get the last insert ID from the Database class?

I have been through a fair bit of the code in modules/database/classes/ but still have not found how to return the last insert Id. How do I get this? ...

How to change one-to-one relationship to one-to-many relationship in MySQL?

I currently have a user's table which contains a one-to-one relationship for Youtube OAuth tokens. However, I now want to support multiple video sites and want to break this into a one-to-many relationship. I have setup the new tables: tokens - cols: id, site, username (the user's username on Youtube), oauth_token, oauth_secret ...

VBA How to find last insert id?

I have this code: With shtControleblad Dim strsql_basis As String strsql_basis = "INSERT INTO is_calculatie (offerte_id) VALUES ('" & Sheets("controleblad").Range("D1").Value & "')" rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic Dim last_id As String last_id = "select last_insert_id()"...

What happens when auto_increment on integer column reaches the max_value in databases?

I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than 2 (or 4) billion posts and integer is not enough? Is the integer overflowed and continue...

php function last_insert_id() is not working with REPLACE INTO query

I am using REPLACE INTO query to insert in to table but after executing query by using mysql_query() function and if I use last_insert_id() it is only giving me 0 value. why this is happening so? and how can I overcome this behavior. :Pankaj ...

PDO lastInsertId issues, php

Hi Guys, I have tried lots of ways to get the last inserted ID with the code below (snipplet from larger class) and now I have given up. Does anyone know howto get PDO lastInsertId to work? Thanks in advance. $sql = "INSERT INTO auth (surname, forename, email, mobile, mobilepin, actlink, regdate) VALUES (:surname, :forename, :ema...

Invalid Argument to getUInt64 when retrieving LAST_INSERT_ID()

I have added a record to my table which auto-increments the primary key. I am having no luck retrieving this new value. The MySQL documents say to use the SELECT LAST_INSERT_ID(); in a query. I have done this, but can't retrieve the results. According the the metadata of the result set, the data type is BIGINT and the column name i...

Weird insert behavior with mysql and codeginiter

I have a fairly simple insert statement <...> if (!empty($attributes)) { $sql = 'INSERT INTO `part_attrs` (`part_id`, `attr`, `type`, `list_order`) VALUES (?, ?, ?, ?)'; foreach($attributes as $key => $attribute) { $this->db->query($sql, array($partid, $attribute[0], $attribute[1], $key)); ...

php retrieving id of the last record inserted and insert it in another table

Hi all, I'm trying to retrieve the id of the last record inserted in the table 'authors' and insert the retrieved id in the 'articles' table; here's my code: $title = mysql_real_escape_string($_POST[title]); $body = mysql_real_escape_string($_POST[body]); $category = mysql_real_escape_string($_POST[category]); $insert_author="INSERT I...

SELECT last id, without INSERT

Hi! I'm trying to retrieve the id of one table A to insert into another table B. I cannot use last_insert_id() as i have not inserted anything into A. Any ideas on how to do this nicely? $n = mysql_query("SELECT max(id) FROM tablename"); doesn't seem to work, nor does $n = mysql_query("SELECT max(id) FROM tablename GROUP BY id"); ...

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 codeigniter specific way? ...

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. ...