mysql insert return id

相關問題 & 資訊整理

mysql insert return id

You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id. Eg: INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...); SELECT LAST_INSERT_ID();. Th, $link = mysqli_connect('127.0.0.1', 'my_user', 'my_pass', 'my_db'); mysqli_query($link, "INSERT INTO mytable (1, 2, 3, 'blah')"); $id = mysqli_insert_id($link);. See mysqli_insert_id() . Whatever you do, , Care, this will work only after your last INSERT query and it will return the first ID only if you have a multiple insert! Then, with the IGNORE option, I don't think that it is possible to get the lines that were not inserted. When you make an INSER, https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row describes the solution perfectly well: connection.query('INSERT INTO posts SET ?', title: 'test'}, function(err, result) if (err) throw err; console.log(result.insertId);, Try this function add_post($post_data) $this->db->insert('posts', $post_data); $insert_id = $this->db->insert_id(); return $insert_id; }. In case of multiple inserts you could use $this->db->trans_start(); $this->db->trans, You could store the last insert id in a variable : INSERT INTO table1 (title,userid) VALUES ('test', 1); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1);. Or get the max id, One way to do this is using INSERT IGNORE : INSERT IGNORE INTO tags (tags,...) VALUES (the_new_tags, ...); SELECT tag_id FROM tags WHERE tags=the_new_tags; ..., Check this page out: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html. At the bottom of the page they explain how you can make LAST_INSERT_ID meaningful for updates by passing an expression to that MySQL function. From the MySQL documentat,If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function. You can check from your C applications whether a value was stored in an AUTO_INCREMENT ,lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP ). The following examples are equal to the examples from the previous page (PHP Insert Data Into MySQL), except that we have added one single line of code to retrieve the ID of the last

相關軟體 MySQL 資訊

MySQL
MySQL 是一個開源的 RDBMS(關係數據庫管理系統),它支持用 C,C ++,Java,Perl 和 PHP 等各種編程語言編寫的請求。由於其高速度和靈活性,MySQL 已成為主要用於開發各種形狀和大小的 Web 應用程序的最流行的數據庫系統之一。自 1995 年上市以來,這種非常受歡迎的開源數據庫管理系統已經應用於當今幾乎所有互聯網用戶的無數項目中。今天一些最受歡迎的 MySQL 用戶是 ... MySQL 軟體介紹

mysql insert return id 相關參考資料
Get the new record primary key ID from mysql insert query? - Stack ...

You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id. Eg: INSERT INTO table_name (col1, col2,...) VALUES ('val1...

https://stackoverflow.com

PHPMySQL insert row then get 'id' - Stack Overflow

$link = mysqli_connect('127.0.0.1', 'my_user', 'my_pass', 'my_db'); mysqli_query($link, "INSERT INTO mytable (1, 2, 3, 'blah')"); $id = mysqli_insert...

https://stackoverflow.com

sql - How do I get the ID of multiple inserted rows in MySQL ...

Care, this will work only after your last INSERT query and it will return the first ID only if you have a multiple insert! Then, with the IGNORE option, I don't think that it is possible to get t...

https://stackoverflow.com

node.js - Retrieve last inserted id with Mysql - Stack Overflow

https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row describes the solution perfectly well: connection.query('INSERT INTO posts SET ?', title: 'test'}, function(err, res...

https://stackoverflow.com

mysql - how to get last insert id after insert query in ...

Try this function add_post($post_data) $this->db->insert('posts', $post_data); $insert_id = $this->db->insert_id(); return $insert_id; }. In case of multiple inserts you could use...

https://stackoverflow.com

insert - LAST_INSERT_ID() MySQL - Stack Overflow

You could store the last insert id in a variable : INSERT INTO table1 (title,userid) VALUES ('test', 1); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO table2 (parentid,otherid,userid...

https://stackoverflow.com

INSERT and return ID, or if DUPLICATE KEY return existing ID in ...

One way to do this is using INSERT IGNORE : INSERT IGNORE INTO tags (tags,...) VALUES (the_new_tags, ...); SELECT tag_id FROM tags WHERE tags=the_new_tags; ...

https://stackoverflow.com

MySQL ON DUPLICATE KEY - last insert id? - Stack Overflow

Check this page out: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html. At the bottom of the page they explain how you can make LAST_INSERT_ID meaningful for updates by passing an expre...

https://stackoverflow.com

MySQL 5.7 Reference Manual :: 27.8.21.3 How to Get the Unique ID

If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function. You can check from your C applic...

https://dev.mysql.com

PHP Get ID of Last Inserted Record - W3Schools

lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP ). The following examples are equal to the examples from the previous page (PHP Insert Data Into MySQL), except that we have added ...

https://www.w3schools.com