How To Retrieve Last Inserted Row ID in WordPress Database?

I have a WordPress plugin that has a table with an AUTO_INCREMENT primary key field called ID. I want to get the ID when I insert a new row. I know there is a PHP function called mysql_insert_id, but I’m not sure how to use it with $wpdb. Is there a better way to get the last inserted row ID from WordPress database?

You can get the last inserted row ID from the WordPress database by using the $wpdb->insert_id property after you execute the $wpdb->insert() method. For example:

$wpdb->insert( $wpdb->prefix . 'my_table', array('name' => 'John', 'email' => 'john@example.com'));
$last_id = $wpdb->insert_id;