{"id":1982,"date":"2004-03-09T01:46:47","date_gmt":"2004-03-09T01:46:47","guid":{"rendered":"http:\/\/www.soulhuntre.com\/items\/date\/2004\/03\/09\/tech-break-mysql-last-inserted-record-index\/"},"modified":"2004-03-09T01:46:47","modified_gmt":"2004-03-09T01:46:47","slug":"tech-break-mysql-last-inserted-record-index","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2004\/03\/09\/tech-break-mysql-last-inserted-record-index\/","title":{"rendered":"Tech break: MySQL last inserted record index?"},"content":{"rendered":"

While working on the website for Client “A” I came across this problem. I need to so a split insert of two newly created records to two tables. In this case a series of “pages” must be linked to one and only one “article”.<\/p>\n

Except of course that when both the article and the initial page are being created at one time, I don’t have the luxury of knowing in advance what the article_id of the article record is so that I can set it in the page_article_id. The article_id field is an automatically incrementing one. The number is not available toll after the insertion has happened.<\/p>\n

<\/p>\n

Now, normally if there was something else unique about the record you could insert it, select it and then read the article_id field, but in this database only the article_id is guarenteed to be unique. I know, not the theoretically perfect way to lay it out but there are many other factors at play. Trust me, this is the best option.<\/p>\n

Fortunately, a little research shows that both MySQL<\/a> and PHP<\/a> have ways for me to access the information I need. Similar abilities exist in all major databases by the way as far as I know, so the principle holds true. <\/p>\n

Anyway, for MySQL it turns out that the easiest method seems to be the [[wp:SQL]] statement LAST_INSERT_ID()<\/a>. This will return the last generated AUTO_INCREMENT<\/a> index. A simplistic example is…<\/p>\n

\n\n\n\n
INSERT INTO foo (auto,text)
    VALUES(NULL,’text’);             <\/p>\n

INSERT INTO foo2 (id,text)
    VALUES(LAST_INSERT_ID()<\/b>,’text’);<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

This is pretty useful, and it seems to be correctly tied to the connection\/session of your context so that a insert by another process in the time between the first and second inserts. You should check and make sure that the first insert actually did<\/i><\/b> insert a record, and you need to be sure that the first insert did trigger a new index or this will not operate as you expect.<\/p>\n

The other option is to use the built in PHP function mysql_insert_id()<\/a> which has some differences from the previous method. <\/p>\n

\n\n\n\n
<?php
    $link = mysql_connect('localhost', 'mysql_user','mysql_password');
    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('mydb');<\/p>\n

    mysql_query(\"INSERT INTO mytable (product) values ('kossu')\");
    printf(\"Last inserted record has id %dn\", mysql_insert_id());
?> <\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

One of the advantages here for my use is that this will fit a little better into the generated PHP code from Dreamweaver<\/a> and it handles some of the details for me as it will return 0 if the previous insert did not generate an index increment. As a bonus, it may be slightly more aware of the PHP session\/connection structure.<\/p>\n

Anyway, I had to put all this here so I could find it in the future, and I figure it might help someone else out \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

While working on the website for Client “A” I came across this problem. I need to so a split insert of two newly created records to two tables. In this case a series of “pages” must be linked to one and only one “article”. Except of course that when both the article and the initial […]<\/p>\n","protected":false},"author":3,"featured_media":53187,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[278],"tags":[],"_links":{"self":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/1982"}],"collection":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/comments?post=1982"}],"version-history":[{"count":0,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/1982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media\/53187"}],"wp:attachment":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media?parent=1982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/categories?post=1982"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/tags?post=1982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}