Ask about php summary 01 + 01 = 02

I want to create an id in a database

id_user => data type 'varchar'

I want my id started with 00

, 01

, 02

etc. And to create a new id, I count all rows and the result from count will be added 01.

Example:

$id=array(00,01,02);
$count_exist_id = $count($id)
$new_id= '00' + $count_exist_id

      

and i hope the new id should be '03'

and it will store the database in the user column of the tableid_user

+3


source to share


2 answers


You can use INT(x) ZEROFILL

to add 0 before the number. '1' => '001'

With INT ZEROFILL you have AUTO_INCREMENT

.;)

CREATE TABLE user (
   id_user INT(8) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT, 
   PRIMARY KEY(id_user)
);

      



If you use UNSIGNED

, you optimize your table and keep one BIT to get more.

See:

+2


source


A simpler approach would be to compute the next id as an integer and then put it on a two-character string:



$id = array(00,01,02);-
$count_exist_id = count($id);
$new_id = str_pad($count_exist_id, 2, '0', STR_PAD_LEFT);

      

+1


source







All Articles