Mysql – Last used index from specific table

indexMySQLrowtable

how can i take last used index from a specific table and add a +1 to it?

database login, table cars

I think i need to add it here ####

$sql = 'INSERT INTO '.$this->options['db_table']
.'
(carID,name, size, type, title, description)'
.' VALUES (#############,?, ?, ?, ?, ?)';

Best Answer

The best way for that is to use the sequence way which is auto increment facility, example:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ENGINE=MyISAM;

so in that case you don't need to mention that id in the insert like:

INSERT INTO animals (name) VALUES
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');

also you can get the inserted id from the function

 LAST_INSERT_ID()

so here the id is auto last rec id + 1, you can see full reference about it from MySQL dev site: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html