Jump to content

Using MySQL_insert_id


Recommended Posts

Try this

local qh = dbQuery(db, 'SELECT LAST_INSERT_ID() AS id') 
local result = dbPoll(qh, -1) 
local id = result[1]['id'] 

You should never use dbPoll with -1 as it freezes the whole thing until it obtains the result, instead use callbacks.

Link to comment

There are several ways to do this, you can also use MAX(). Another way you can do to make sure you get the last one ( if you don't trust MAX() or LAST_INSERT_ID() ) you can make a query that selects everything, or just the id and order it by ID, DESC ( descend ) and limit it with; limit 0,1. :)

Another thing, mysql is a database, where your database is being stored. Quite an inception, and Database is being built by another database ( you can read all about it on the internet ). An auto_increment will never reset, the only actual way to do it is to set it yourself. You can either make it an int and make your own increment or select the last id and overwrite the empty id.

Link to comment
Try this
local qh = dbQuery(db, 'SELECT LAST_INSERT_ID() AS id') 
local result = dbPoll(qh, -1) 
local id = result[1]['id'] 

You should never use dbPoll with -1 as it freezes the whole thing until it obtains the result, instead use callbacks.

If that should not be used then what is a better way to do it?

There are several ways to do this, you can also use MAX(). Another way you can do to make sure you get the last one ( if you don't trust MAX() or LAST_INSERT_ID() ) you can make a query that selects everything, or just the id and order it by ID, DESC ( descend ) and limit it with; limit 0,1. :)

Another thing, mysql is a database, where your database is being stored. Quite an inception, and Database is being built by another database ( you can read all about it on the internet ). An auto_increment will never reset, the only actual way to do it is to set it yourself. You can either make it an int and make your own increment or select the last id and overwrite the empty id.

Sorry I did not follow. What is MAX() and how would it be used in a script? In your second paragraph I understand what you are saying, so I am asking for a alternative to it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...