last-insert-id

getting last inserted row id with PDO (not suitable result)

偶尔善良 提交于 2019-12-06 14:31:40
I have a problem with PDO::lastInsertId() method which doesn't return the id (primary key) of last inserted row, instead it returns another field which is a foreign key field. PHP code: $pdo = new PDO(...); $stmt = $pdo->prepare($sql); $stmt->bindParam(...); $stmt->bindParam(...); $stmt->execute(); $id = $pdo->lastInsertId(); // or $id = $pdo->lastInsertId('services_id_seq'); // I think 'services_id_seq' is not necessary in MySQL // both of them don't return the primary key of last inserted row echo 'last inserted id: ' . $id; MySQL Table structure: ... id int unsigned not null primary key

why pdo->lastInsertId() return 0 when i call STORED PROCEDURE in mysql?

女生的网名这么多〃 提交于 2019-12-05 19:23:43
i need return id of last inseted row in my database : i have a class named DatabaseHandler to use pdo <?php class DatabaseHandler { private static $_mHandler; private function __construct() {} private static function GetHandler() { if (!isset(self::$_mHandler)) { try { self::$_mHandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => DB_PERSISTENCY)); self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$_mHandler->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); } catch (PDOException $e) { self::Close(); trigger_error($e->getMessage

How to get inserted id using Spring Jdbctemplate.update(String sql, obj…args)

こ雲淡風輕ζ 提交于 2019-12-05 11:12:56
I'm using Jdbctemplate and I need the inserted id of a query. I read that I have to build a particular PreparedStatement and use GeneratedKeyHolder object. The problem is that in my application all inserts method uses this JdbcTemplate update method: getJdbcTemplate().update(SQL_INSERT,param1,param2,param3,...); Is there another way to get the inserted id without refactoring all daos? Looking at the documentation for NamedParameterJdbcTemplate and JdbcTemplate You have two choices: use NamedParameterJdbcTemplate 's update method. use JdbcTemplate 's update method. There are also some other

PDO Get Multiple Insert Ids

[亡魂溺海] 提交于 2019-12-05 10:45:43
Running the following query using PDO (Actually, I use prepared statements but same problem) INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2') How can I get the Ids for the records relating to Row1 and Row2 ? $db->lastInsertId() literally returns the last single Id. Is it sufficient to take this last Id, subtract the # of records and assume that range covers all my records? can there be gaps/jumps. Is this query guaranteed to be atomic? If you're using MyISAM tables then because of the table level locking mechanism it's only possible for you to get given a range of ids. After reading http:

How to retrieve all last inserted rows IDs in mysql-php? [closed]

空扰寡人 提交于 2019-12-05 09:39:12
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . Closed 6 years ago . Is there any efficient way to retrieve all last inserted rows auto incremented IDs from mysql database using php? Suppose I don't know how many rows are there to be inserted in a batch but I need all of their IDs after insertion. After Googling I didn't find any efficient way to do this, so I'm not sure is that

PDO - lastInsertId() for multiple insert query

你离开我真会死。 提交于 2019-12-05 02:46:54
I can insert 2 pets into a table, and get their lastInsertId() for further processing one at a time (2 queries). I am wondering if there is a way to get two lastInsertIds() and assign them to variables if I am inserting 2 rows in 1 query: $query = "INSERT INTO pets (pet_name) VALUES (':coco'),(':jojo')"; $pet_insert = $dbh->prepare($query); $pet_insert->execute(array(':coco' => $coco,':jojo' => $jojo)); $New_PetID = $dbh->lastInsertId(); Is it possible to get the lastInsertId() for coco and for jojo? So something like: $New_PetID1 = $dbh->lastInsertId();//coco $New_PetID2 = $dbh->lastInsertId(

PHP Postgres: Get Last Insert ID

限于喜欢 提交于 2019-12-04 17:47:00
问题 I found a couple of other questions on this topic. This one... mysql_insert_id alternative for postgresql ...and the manual seem to indicate that you can call lastval() any time and it will work as expected. But this one... Postgresql and PHP: is the currval a efficent way to retrieve the last row inserted id, in a multiuser application? ...seems to state that it has to be within a transaction. So my question is this: can I just wait as long as I like before querying for lastval() (without a

When I INSERT multiple rows into a MySQL table, will the ids be increment by 1 everytime?

自作多情 提交于 2019-12-04 03:11:26
if I have a query like the following: INSERT INTO table (col1,col2,col3) VALUES ('col1_value_1', 'col2_value_1', 'col3_value_1'), ('col1_value_2', 'col2_value_2', 'col3_value_2'), ('col1_value_3', 'col2_value_3', 'col3_value_3'); Suppose that I have a table where the last id PRIMARY_KEY AUTO_INCREMENT value is 56 , then will this insert query always create 3 records with ids 57, 58, 59 . Is this operation atomic? Or, if another query writes on the same table, could the ids not increment always by 1? Thanks for the attention! EDIT : Please read the following because maybe I wasn't so clear. Of

Mysql get last id of specific table

ぃ、小莉子 提交于 2019-12-03 07:21:04
问题 I have to get last insert id from a specific inserted table?. Lets say i have this code: INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2'); INSERT INTO blahblah2 (test1, test 2) VALUES ('test1', 'test2'); INSERT INTO blahblah3 (test1, test 2, lastid) VALUES ('test1', 'test2', last id of blahblah); How do i get the insert id of table blahblah in table blahblah3? LAST_INSERT_ID() only gives you the last insert id Regards, Simon :) 回答1: You can use LAST_INSERT_ID() function. Try

MySQL SELECT LAST_INSERT_ID() for compound key. Is it possible?

↘锁芯ラ 提交于 2019-12-02 00:21:37
问题 Can I get the LAST INSERT ID() for a compound key in MySQL? 回答1: Yes. You can't have multiple auto-increment fields in a single table. CREATE TABLE foo ( id1 int(11) NOT NULL auto_increment, id2 int(11) NOT NULL default '0', PRIMARY KEY (id1, id2) ); INSERT INTO foo VALUES (DEFAULT, 2); SELECT LAST_INSERT_ID(); -- returns 1, the value generated for id1 LAST_INSERT_ID() returns the value only for a column declared AUTO_INCREMENT . There's no function to return the value in a compound primary