Questions tagged [lastinsertid]

135 questions
9
votes
2 answers

vb.net sql last inserted ID

i'm using VB.NET with an Access Database, I insert values but then I need to get the last inserted ID (auto number) and insert that into a related table. I have tried @@IDENTITY and MAX(column) but @@IDENTITY returns zero and MAX isn't very…
Elliott
  • 3,522
  • 23
  • 66
  • 93
8
votes
2 answers

MySQL UUID primary key - generated by PHP or by MySQL?

I was under the impression that just having MySQL generate the primary key via UUID() would make the key unique across servers, etc. But, there is no way to fetch the last inserted UUID, which requires that an extra select statement be done each…
ina
  • 17,912
  • 37
  • 113
  • 187
8
votes
7 answers

Using LAST_INSERT_ID() via PHP?

When I execute the following in the MySQL console, it works fine: INSERT INTO videos (embed_code) VALUES ('test code here'); SELECT LAST_INSERT_ID(); But, when I execute the above queries via PHP, the result is empty. Under the data abstraction, I…
Mike Moore
  • 6,398
  • 8
  • 38
  • 63
8
votes
3 answers

How to detect the last insert ID within a transaction in Yii using DAO?

That's the source code, I need to detect the ID (see the marked position between the two queries below). $connection = Yii::app()->db; $transaction=$connection->beginTransaction(); try { $q = "INSERT INTO `someTable1` .... "; …
user3702874
  • 83
  • 1
  • 3
8
votes
4 answers

lastInsertId does not work in Postgresql

I am using Postgresql, when I want to use PDO to retrieve the latest insertion ID, I got a problem. Here is my code: $db->lastInsertId('columnName'); The error message says SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "columnName" does not…
Michael
  • 1,995
  • 7
  • 29
  • 46
7
votes
1 answer

PDO lastInsertId issues, php

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,…
Kyle Hudson
  • 868
  • 1
  • 14
  • 26
6
votes
2 answers

MySQL+PHP: getting last_id of multiple/composite primary key

I need to get the last inserted id of table that have multi-column primary keys. Those tables does not have AUTOCOUNT column. I'm using parametrized queries (arbitrary order) Using PHP (5.3) and MySQLi module Arbitrary INSERT SQL Query. (In any…
lepe
  • 22,543
  • 9
  • 85
  • 99
6
votes
1 answer

For the method PDO::lastInsertId(), what do they mean by the argument "sequence name"? (PHP - PDO)

I am trying to use PDO's lastInsertId method, but the documentation states that for some rdbms I need a sequence name as an argument. Only being familiar with mysql, I am not quite sure what a sequence name is. Do they mean the name of the column…
dqhendricks
  • 17,461
  • 10
  • 44
  • 80
6
votes
2 answers

Fastest way to insert, if not exist, then get id in MySQL

There's this table. | id | domain | id is the primary key. domain is a unique key. I want to: Insert a new domain, if it doesn't exist already. Get the id for that domain. Now I'm doing it like this: INSERT INTO domains SET domain =…
Znarkus
  • 21,120
  • 20
  • 71
  • 104
6
votes
2 answers

Mysql mulitple row insert-select statement with last_insert_id()

Ok. So the short of it is, I was trying to do an INSERT SELECT such as: START TRANSACTION; INSERT INTO dbNEW.entity (commonName, surname) SELECT namefirst, namelast FROM dbOLD.user; SET @key = LAST_INSERT_ID(); INSERT INTO dbNEW.user…
edl
  • 3,054
  • 20
  • 21
6
votes
2 answers

ON DUPLICATE KEY UPDATE doesn't work when there's an UPDATE trigger

I have an INSERT statement that looks like this: INSERT INTO officer (officer_number, name, bank_id) VALUES ('', '', 8) ON DUPLICATE KEY UPDATE officer_number = '', …
Jason Swett
  • 38,405
  • 60
  • 193
  • 322
6
votes
5 answers

Fetching last insert id shows wrong number

I have table with three records. There is one filed as auto_increment. The ID's are 1, 2, 3... When I run query SELECT LAST_INSERT_ID() FROM myTable The result is 5, even the last ID is 3. Why? And what is better to use? LAST_INSERT_ID() or SELECT…
Josef
  • 2,430
  • 5
  • 29
  • 56
6
votes
6 answers

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…
BlaM
  • 26,721
  • 31
  • 89
  • 104
6
votes
2 answers

How to get last inserted row id from sqlite in phonegap android

I am creating an app in which i am inserting a data in the table. After inserting the data in the table i want to get the id of that row so i can do further operation depending on it. I tried to use last_insert_rowid() function of sqlite but found…
Neerav Shah
  • 697
  • 4
  • 16
  • 38
5
votes
5 answers

Using MYSQL LAST_INSERT_ID() to retrive the ID of inserted row?

I have a table which has got a column with AUTO INCREMENT. I have to retrieve the value of this column when inserting a new row (i need the value of the new row). I've read a lot about it and found different solutions, one is to use SELECT…
AlexanderNajafi
  • 1,551
  • 2
  • 22
  • 37
1
2
3
8 9