lastinsertid

mysqli last insert id

*爱你&永不变心* 提交于 2021-01-29 15:56:41
问题 I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id; then binding but it doesn't work. Can someone help me out? $image = $mysqli->insert_id;//this should come from table2 $stmt = $mysqli->prepare(" insert into table1 (username, firstname, lastname, image) select ?,?,?,image from table2 t2 where username = ? and t2.id = ? "); $stmt->bind_param('sssss', $username, $fname,

Fetching last insert id shows wrong number

大憨熊 提交于 2020-12-29 12:27:13
问题 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 MAX(ID) FROM myTable? 回答1: last_insert_id() has no relation to specific tables. In the same connection, all table share the same. Below is a demo for it. Demo: mysql> create table t1(c1 int primary key auto_increment); Query OK, 0 rows affected (0

Fetching last insert id shows wrong number

一曲冷凌霜 提交于 2020-12-29 12:25:59
问题 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 MAX(ID) FROM myTable? 回答1: last_insert_id() has no relation to specific tables. In the same connection, all table share the same. Below is a demo for it. Demo: mysql> create table t1(c1 int primary key auto_increment); Query OK, 0 rows affected (0

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

谁都会走 提交于 2020-01-12 05:11:09
问题 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? 回答1: 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

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

五迷三道 提交于 2019-12-30 08:20:09
问题 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();

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

天大地大妈咪最大 提交于 2019-12-30 07:18:09
问题 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

pdo lastInsertId returns zero(0)

Deadly 提交于 2019-12-29 01:46:14
问题 All queries execute successfully, when I check table in MySQL row inserted successfully without any error, but lastInsertId() returns 0 . why? My code: // queries executes successfully, but lastInsetId() returns 0 // the menus table has `id` column with primary auto_increment index // why lastInsertId return 0 and doesn't return actual id? $insertMenuQuery = " SELECT @rght:=`rght`+2,@lft:=`rght`+1 FROM `menus` ORDER BY `rght` DESC limit 1; INSERT INTO `menus`(`parent_id`, `title`, `options`,

Why does PDO::lastInsertId return 0?

▼魔方 西西 提交于 2019-12-24 01:17:54
问题 This is my code. // insert reward into wallet $sql = " INSERT INTO `wallet` (`uid`, `created_at`, `amount`, `type`, `payment_id`) VALUES (:uid, CURRENT_TIMESTAMP, :amount, 'payment', :payment_id); "; $sth = self::link()->prepare($sql); // primary key makes sure payment does not get double rewarded $sth->execute( array( ':uid' => $referer, ':amount' => $reward, ':payment_id' => $payment_data['payment_id'], ) ); var_dump(self::link()->errorInfo()); self::log("issuing subscription",self::LOG

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

99封情书 提交于 2019-12-23 02:58:06
问题 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); }); 回答1: 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

Race Conditions with mysql_last_id()

倾然丶 夕夏残阳落幕 提交于 2019-12-22 09:40:02
问题 I've spent the better part of the day trying to find an answer to this, but there just doesn't seem to be one out there. I have need of function which creates a row in a table, retrieves the auto-increment from the inserted row, and then uses that value to insert another row in a second table. Something functionally similar to the following php: $mysql_query("INSERT INTO table1 SET columns='values'"); $id = mysql_insert_id(); $mysql_query("INSERT INTO table2 SET id='$id', columns='values'");