4

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vincent
  • 852
  • 6
  • 29
  • 67

2 Answers2

3

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')
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • when i set the item_id it will be like this? item_id unsigned not null auto_increment = "00000001", ? – Vincent Mar 17 '14 at 03:39
  • 1
    @Vincent . . . The initial value is 1, so you don't need to set it. And, the leading zeros are useless, because the column is numeric. You can put them in when you query the value. – Gordon Linoff Mar 17 '14 at 03:40
  • but i need the other seven zeros, i will show it in my other html pages – Vincent Mar 17 '14 at 03:42
  • 1
    That is why I have the "Note". You can pull it out with the leading zeroes by using `lpad()`. If you think about it, you cannot "increment" a string value. But, you can increment a number and turn it into a string. – Gordon Linoff Mar 17 '14 at 03:46
  • 1
    @GordonLinoff I haven't used ZEROFILL in conjunction with PHP, but wouldn't `item_id INT(8) UNSIGNED NOT NULL ZEROFILL AUTO_INCREMENT;` work for capturing it as a padded value in PHP? EDIT: Wow, old thread. Not sure how / why this popped up on recent questions. – Noah May 02 '14 at 21:34
-1

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

Jack
  • 436
  • 5
  • 13