Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ask about php summarize 01 + 01 = 02

Tags:

sql

php

mysql

I want to make a id in database

id_user => data type 'varchar'

I want my id to start from 00, 01, 02, and so on. And to make new id, I count the all the rows, and the result from count will be added by 01.

Example:

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

and I hope the new id must be '03' and it will be store to database in table user column id_user

like image 552
nanasjr Avatar asked Mar 18 '26 09:03

nanasjr


1 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 you save one BIT, to get bigger number.

See :

  • What is the benefit of zerofill in MySQL?
  • How can I set autoincrement format to 0001 in MySQL?
  • What does "unsigned" in MySQL mean and when to use it?
like image 151
Sky Voyager Avatar answered Mar 20 '26 01:03

Sky Voyager



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!