Grab last key from auto increment PDO

Asked

Viewed 1,036 times

2

I have a problem with my system that is the following,

try {
$sql = "SELECT Nid+1 AS Nid  FROM noticias ORDER BY Nid DESC LIMIT 1";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":Nid", intval($_GET["Nid"]));

$stmt->execute();
$results = $stmt->fetchAll();
$Npasta = $results[0]["Nid"];

} catch (Exception $ex) {
echo $ex->getMessage();
}

I am taking the last registered ID and creating a folder with the same number but when I delete the last id the count becomes wrong because EX: I entered the id 998, after inserting deleted, the next that I will insert it will get 998 again , where the right was to get the id 999; Some light on that?

3 answers

2

The two less bad exits to this problem, which I see are:

1) Create a column called excluido or ativo can be bit or Boolean, instead of deleting the record, update this column, this partially solves the problem of holes in the sequence.

2) Create a new table with two columns an id and the other the value of the last Insert, for each record record in its main table and this new one and always read the last value in this table.

  • Instead of this column, in a college paper, I was instructed to do an extra audit table, where I would save anything that was excluded. This suggestion is also good?

  • 1

    @Diegofelipe, the extra table of your work seems to me to be in another context, in the log operations. In the reply suggest it to avoid a delete, so to bring all news 'not deleted' would be something like this, select * from noticias where excluida = false

2

If the table has a primary key with autoincrement you can consult the value of the next record that will be inserted with the query below, because the value of AUTO_INCREMENT is not amended by line exclusions.

SELECT `AUTO_INCREMENT`
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'noticias';

That way should make the same effect as Nid+1 of the question.

  • Seems like the best option :)

  • That’s right I’ll test it here

  • more here I did not understand how I will select the field I get the ID because at no time at your query you informed which is the field ( in my case Nid). SELECT AUTO_INCREMENT FROM table that Where serves for q?

  • 1

    it returns the value of the property AUTO_INCREMENT table, the field is not mentioned in the query because it depends on the definition of the fields, in your case it will work if the field Nid own the property AUTO_INCREMENT active

0


searching on the subject I found this method that is in my opinion the safest because it takes directly the value of auto_increment follows the final code of the solved problem

try {
$sql = "SHOW TABLE STATUS LIKE 'nomedatabela' ";  
$stmt = $DB->prepare($sql);
$stmt->execute();
$resultado = $stmt->fetch();
$proximoID = $results['Auto_increment'];  // a chave esta aqui
} catch (Exception $ex) {
 echo $ex->getMessage();
}
echo "$proximoID";

Done even if the id is deleted it will always fetch the next one to be inserted

  • Safe is not, if you have two tables with similar names ...

  • 1

    When I said safe, I said in order to really get the next ID in the safest way possible because it is the own auto increment, as the query can also become safer if use another method

Browser other questions tagged

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