How do I get the last inserted ID of a MySQL table in PHP?

后端 未结 16 2305
温柔的废话
温柔的废话 2020-11-21 23:09

I have a table into which new data is frequently inserted. I need to get the very last ID of the table. How can I do this?

Is it similar to SELECT MAX(id) FROM

16条回答
  •  醉梦人生
    2020-11-21 23:43

    Using MySQLi transaction I sometimes wasn't able to get mysqli::$insert_id, because it returned 0. Especially if I was using stored procedures, that executing INSERTs. So there is another way within transaction:

    insert_id;
    
        $result = $instance->query('SELECT LAST_INSERT_ID();');
    
        if($instance->errno)return false;
    
        list($buffer) = $result->fetch_row();
    
        $result->free();
    
        unset($result);
    
        return $buffer;
    }
    
    ?>
    

提交回复
热议问题