Author Topic: Getting the current insert ID  (Read 1034 times)

0 Members and 1 Guest are viewing this topic.

Offline KryptixTopic starter

  • Enthusiast
  • Posts: 269
  • Gender: Male
    • View Profile
    • RuneScape Classic
Getting the current insert ID
« on: March 17, 2010, 11:49:33 PM »
I'm trying to automatically insert a new topic on a FluxBB forum but I'm running into problems.

INSERT INTO `posts` (`poster`, `poster_id`, `message`, `posted`, `topic_id`) VALUES ('RSCEmulation', 19821, 'Here's my post', UNIX_TIMESTAMP(), (SELECT MAX(`id`) + 1 FROM `topics`));

INSERT INTO `topics` (`poster`, `subject`, `posted`, `last_post`, `last_post_id`, `last_poster`, `forum_id`) VALUES ('RSCEmulation', 'Here's my topic', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), (SELECT MAX(`id`) FROM `posts`), 'RSCEmulation', 37);


Both `posts` and `topics` use each others ID (which is a primary key and automatically incremented) to link together. When inserting the entry I need to get the corresponding ID.

Someone in IRC said to use the above but it's not working, it works OK but if I delete a record the expected ID is 2 but the above returns 0 (and continues increasing the more rows that have been deleted).

I'm also writing this in Java so it would help if I don't use any PHP functions.

Cheers guys.

Offline KryptixTopic starter

  • Enthusiast
  • Posts: 269
  • Gender: Male
    • View Profile
    • RuneScape Classic
Re: Getting the current insert ID
« Reply #1 on: March 18, 2010, 12:04:35 AM »
I found this post but it returns an array which I don't know how I can use in the middle of a SQL query.

Offline KryptixTopic starter

  • Enthusiast
  • Posts: 269
  • Gender: Male
    • View Profile
    • RuneScape Classic
Re: Getting the current insert ID
« Reply #2 on: March 18, 2010, 12:46:00 AM »
LAST_INSERT_ID(`id`) is the same as MAX(`id`).  :-[

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Getting the current insert ID
« Reply #3 on: March 18, 2010, 05:01:25 AM »
You need three queries for that.
1. Insert a new topic - with no post information for time being
2. Insert a new post using LAST_INSERT_ID() to link it to topic
3. Update topic with post information.

And no: LAST_INSERT_ID() is not the same as MAX(id) (although it often gives same result).
See mysql_insert_id()

[edit]
Just noticed you do this in Java :P

Anyway, Java probably has an eqivalent of PHP's mysql_insert_id() - what package do you use to connect to database?
« Last Edit: March 18, 2010, 05:06:32 AM by Mchl »
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Getting the current insert ID
« Reply #4 on: March 22, 2010, 04:28:04 PM »
LAST_INSERT_ID(`id`) is the same as MAX(`id`).  :-[
That's not even close to being true in the general case.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline KryptixTopic starter

  • Enthusiast
  • Posts: 269
  • Gender: Male
    • View Profile
    • RuneScape Classic
Re: Getting the current insert ID
« Reply #5 on: May 07, 2010, 04:36:56 AM »
I meant it gave the exact same result.