Vincent
Vincent

Reputation: 852

how to auto-increment a value in php?

I'm new to php and when I add data in my table, the item_id will be AUTO INCREMENT and it must start at 00000001 what php function will I use ?

item_Id | item_description
00000001| Samsung galaxy s3

and when I add another item it will be something like this:

 item_Id | item_description
 00000001| Samsung galaxy s3
 00000002| Remote Controller

I'm using codeigniter.

Upvotes: 4

Views: 3793

Answers (2)

Jack
Jack

Reputation: 446

since it's an item ID and not used in calculations just use VARCHAR and format the number to add leading zeros and convert to string http://php.net/manual/en/function.sprintf.php

Upvotes: -1

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You do auto-incrementing in the database, when you define the table:

create table items (
    item_id unsigned not null auto_increment,
    . . .
);

When you insert an item, just insert all other columns besides the item_id:

insert into items(col1, . . . )
    . . .

The database will set the item_id to a new value whenever new values are inserted.

Note: the inserted value is an integer. If you want to pull it out as a zero-padded string, you can do:

SELECT LPAD(item_id, 8, '0')

Upvotes: 3

Related Questions