Calculate ID from Database

Asked

Viewed 36 times

0

I am maintaining on a PHP system with Mysql. I noticed that in the user table there is a field called code. This field stores as follows:

id_usuario | codigo_usuario
   1            00001

And the last value:

   id_usuario | codigo_usuario
       7810         07810

How could I calculate, can it be in PHP or within the table itself so that the next records keep this pattern? Ex:

  id_usuario | codigo_usuario
     7811          07811
  • Basically making a LPAD in id with length 5, but when it reaches 99999, which will happen after?

  • It is.... the type of this field is int(5).

  • changed to int(11).

  • If he is int, there will be no such zeros on the left, so it will be the same value as id.

  • Right. He’s that way: codigo_usuario int(5) UN zerofill . It seems that the colleague attributed this value.

1 answer

1


You can do it in your php as follows:

1) do the Insert on your bank

2) Then take the last id inserted with the function below

`$ultimoId= mysqli_insert_id($con)`;

3) then put the zeroes on the left with the following Cod:

$cod_usuario=sprintf("%05d", $ultimoId);

4) Update your table where you seven $cod_usuario where the id goes $ultimoId

  • Thank you Jasar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.