lastinsertid

How do I get the Last Insert Id from a JayData transaction?

烈酒焚心 提交于 2019-12-06 17:42:29
Using JayData to insert a new object (record) into a Sqlite database, how do I query for the last insert id? I can do it like this but it seems inelegant: DB.Members.orderByDescending('member.Id').take(1).toArray(function(member){ alert(member.Name); }); If you have defined the Member class with auto-incremented Id, there is no additional query you have to write, because the Id property is filled automatically after the saveChanges() . To define a auto-incremented Id to your class, check if you have written this: $data.Entity.extend("Member", { Id: { type: "int", key: true, computed: true }, .

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(

MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()

北城以北 提交于 2019-12-03 20:44:41
问题 From what I have read LAST_INSERT_ID() retrieves the id of the last insert statement to run. If that the last insert statement run on your connection or the last insert run on the database for all connections? I guess what I am trying to ask is: on a busy database driven website what are the chances the following code would return the wrong id, and is there a method to prevent that other then locking the database? INSERT INTO example (string) VALUE ('something'); SELECT LAST_INSERT_ID();

Alternative to “PDO::lastInsertId” / “mysql_insert_id”

你说的曾经没有我的故事 提交于 2019-12-03 14:36:41
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 when a TRIGGER inserts into another table with auto-increment: // -> Returns newly created ID of trigger's INSERT $id = $DB->lastInsertId(); What's the alternative? If you go the route of ADOdb ( http://adodb.sourceforge.net/ ), then you can create the insert ID

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

六眼飞鱼酱① 提交于 2019-12-03 07:54:25
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 time I insert. Is it possible to have PHP generate the exact same UUID() that MySQL would generate? Colin O'Dell No, it's not possible to have PHP generate the exact same UUID() as MySQL because it's a (completely) random number. It sounds like your problem is that you like using UUID() in MySQL but don't want to execute an extra query to figure out

last_insert_id() vs SELECT Max(ID)

让人想犯罪 __ 提交于 2019-12-02 02:13:52
问题 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. 回答1: Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id() Definitely not, never! LAST_INSERT_ID() exists exactly for the reason you state: Other clients could have made other inserts. LAST_INSERT_ID() always gives you the last inserted ID on the current connection. mySQL

Two inserts in PHP/MySQL using LAST_INSERT_ID() and rows from another table

放肆的年华 提交于 2019-12-01 12:59:48
I am working on an events calendar using PHP and MySQL (V5.1) where the admin would add an event, and then an attendance list for that event would be created as well. I have three tables: events, attendance and members. So far I can create the event using information that is entered thorugh a PHP form. I'm trying to update the attendance table by inserting the event id from the event that has just been created, as well as pulling in the list of members (using their member ID) from the members table. Nothing is being added to attendance the table though. Can someone let me know what I should I

Two inserts in PHP/MySQL using LAST_INSERT_ID() and rows from another table

落花浮王杯 提交于 2019-12-01 09:53:37
问题 I am working on an events calendar using PHP and MySQL (V5.1) where the admin would add an event, and then an attendance list for that event would be created as well. I have three tables: events, attendance and members. So far I can create the event using information that is entered thorugh a PHP form. I'm trying to update the attendance table by inserting the event id from the event that has just been created, as well as pulling in the list of members (using their member ID) from the members

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

纵然是瞬间 提交于 2019-12-01 03:47:50
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` .... "; $connection->createCommand($q)->execute(); // Single Row Inserted // HERE!! How to get the last insert ID from query above $q = "INSERT INTO `someTable2` .... WHERE id = LAST_INSERT_ID_FROM_FIRST_QUERY "; $connection->createCommand($q)->execute(); $transaction->commit(); } catch (Exception $e) { // react on exception $trans->rollback(); } What would be the most suitable

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

拟墨画扇 提交于 2019-11-30 23:13:01
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 this last insert id for further processing of other records and making subsequent entries in the related tables. Is this approach safe enough or am I missing something. Please help me formulate a proper working plan for this so that I can use the last insert id for