General Community > Scripting Help
SMF1 Trying to get Auto_increment value.
Arantor:
Why can't you, for instance, use mysql_insert_id() to fetch the last inserted id into a table?
MrMorph:
Sure there may be a better way of doing it, but for the moment I am happy to use my method. It's quick and easy and in standalone tests it works fine for what I am doing. I think mysql_insert_id() relates to the previous query yes ? In my project that would mean storing that value some place each time because when I need it isn't necessarily at the time of the last INSERT.
Thanks for the tip though, I will give it some more thought :)
Arantor:
You know that if a column is auto-increment, you don't actually need to know the value in order to insert it, right? You can do the insert then get the newly used id for whatever else you need to use it.
SMF itself does this, for example, for a new topic, it inserts the message, fetches the new message id, inserts the new topic, fetches that topic id, then updates the message with it.
The problem with relying on the auto-increment value yourself is that depending on activity, that other id can be taken up while you were busy (which is the reason why you also don't do SELECT MAX(id) FROM table to get the last currently-used one)
MrPhil:
In one place you say that your $db_prefix is 'smf1', and in another you say your actual table name is 'merc_message_log', which means $db_prefix is probably 'merc_'. 'message_log' is not a standard SMF table, but I'm assuming that you've got it named in the same manner as the standard tables. Is 'smf1' actually the database name? It might have your account name prefixed to it for the Settings.php entry $db_name.
For updating the auto_increment field, just insert with a null for that field, or omit that field entirely (if you list the fields you do give by name). I agree that you run a great risk of having a duplicate ID by trying to specify it explicitly, which will lead to a duplicate key error. All you're trying to do is insert a new record with a unique ID?
Arantor:
Actually, no, he's right. The modified $db_prefix is just the DB name (go look at the SSI code, it reinserts the DB name at the start of the prefix, and there isn't a prefix after the DB name in his example)
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version