catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

$database->id() returns NULL #902

Closed harryqt closed 4 years ago

harryqt commented 4 years ago

Describe the bug

$database->id() returns NULL even after a successful insert()

Information

Detail Code

$database = new Medoo\Medoo([
    'database_type' => 'mysql',
    'database_name' => 'dbname',
    'server'        => 'localhost',
    'username'      => 'root',
    'password'      => ''
]);

$query = $database->insert('table_copy', [
    'id'           => 59093903,
    'api_response' => serialize(str_repeat('test', 5))
]);

// Here the lastInsertId() returns NULL which is incorrect.
var_dump($database->id());

// returns "0" (string) which is also incorrect.
var_dump($database->pdo->lastInsertId());

// However rowCount() returns 1 (int) which is correct.
var_dump($query->rowCount());
catfan commented 4 years ago

The id() or lastInsertId() from PDO, is for returning the auto-increase id last inserted. The id of non-key or not auto-increase column will not be return.

You may need to know how database engine works.