MySQL query in PHP gives an obvious incorrect result

I use PHP and PHPMyAdmin to create a small profile site. I give members an identification number based on what is the largest number currently in the database, +1

I did 25 tests before I got a PHP script where I would like.

Then I deleted those 25 entries using PHPMyAdmin.

But now that my PHP code is doing this:

function getLatestID() {
    $query = "SELECT max(member_id) FROM members";
    $result = @mysql_query($query) or showError("unable to query database for user information");
    if (!($record = mysql_fetch_array($result))) return null;
    return $record[0];
}

I get the wrong number.

Testing scenario: the database table contains 3 records, with identifiers 1, 2 and 3.

I start a debugging session and put a breakpoint in return $record[0]. I check its contents and instead of 3, which is the largest number, it is 28.

As in 25 + 3 = 28, 25 records that I have already deleted ...

Does anyone know what causes this and how can I fix it?

+3
3

, , auto_increment, . , , , reset .

+6

auto_increment MySQL, .

TRUNCATE TABLE mytable - reset .

+5

, - :

ALTER TABLE members AUTO_INCREMENT = 3;

, , auto-increment, MAX + 1. , , :

SHOW CREATE TABLE members;

"AUTO_INCREMENT = 26" .

+3
source

Source: https://habr.com/ru/post/1704775/


All Articles