MySQL-PHP - ID auto_increment does not increase by 1

0

My web application registers a user and saves it in the BD but the id's go 4-9-18-27-48-57-66-75. How do you make the auto increment to be 1? Why can this be happening?

This is the function that inserts the new $ user in the table: * the function receives $ _POST as parameter $ user.

function guardarUsuarioDB($usuario) { useDB(); global $db; $sql = "INSERT INTO users VALUES (default,:name, :email, :password)"; $query = $db->prepare($sql); $query->bindValue(":name", $usuario["name"]); $query->bindValue(":email", $usuario["email"]); $query->bindValue(":password", $usuario["password"]); $query->execute(); $usuario["id"] = $db->lastInsertId(); return $usuario; }

    
asked by Die Duro 16.10.2017 в 21:11
source

2 answers

0

In MySQL nothing ensures that the id that is generated automatically increases by 1 in 1 unless you specify it. Internally Mysql uses sequences to set the IDs. This means that if the moment arrives in the sequence, for whatever reason there are previous values that have been left free, it is possible to reuse them instead of generating a new id.

That is why one of the principles in the design of databases is that the field that represents the identifier of a tuple of data is completely meaningless, because if you do not force yourself to have control over the same, this value does not have to be sequential.

    
answered by 16.10.2017 / 22:51
source
0

The first thing you have to do is make sure that your table has been created as the documentation says:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

Source: link

And in the second it verifies if by inserting it directly into the database, if it increases normally, otherwise something could go wrong in your prepared query.

    
answered by 16.10.2017 в 22:49