> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/connectors/mariadb-connector-c/api-functions/mysql_insert_id.md).

# mysql\_insert\_id

## Syntax

```c
my_ulonglong mysql_insert_id(MYSQL * mysql);
```

## Parameter

* `mysql` - a mysql handle, which was previously allocated by [mysql\_init()](/docs/connectors/mariadb-connector-c/api-functions/mysql_init.md) or [mysql\_real\_connect()](/docs/connectors/mariadb-connector-c/api-functions/mysql_real_connect.md).

## Description

The mysql\_insert\_id() function returns the ID generated by a query on a table with a column having the [AUTO\_INCREMENT](/docs/server/reference/data-types/auto_increment.md) attribute or the value for the last usage of [LAST\_INSERT\_ID(expr)](/docs/server/reference/sql-functions/secondary-functions/information-functions/last_insert_id.md). If the last query wasn't an [INSERT](/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert.md) or [UPDATE](/docs/server/reference/sql-statements/data-manipulation/changing-deleting-data/update.md) statement or if the modified table does not have a column with the [AUTO\_INCREMENT](/docs/server/reference/data-types/auto_increment.md) attribute and [LAST\_INSERT\_ID](/docs/server/reference/sql-functions/secondary-functions/information-functions/last_insert_id.md) was not used, this function will return zero.

## Return Value

Returns the value of the modified column with `AUTO_INCREMENT` attribute. If the table doesn't contain an `auto_increment` column or no INSERT/UPDATE statement was executed, this function will return zero.

{% hint style="info" %}
When performing a multi-insert statement, `mysql_insert_id()` will return the value of the first row.
{% endhint %}

## See Also

* [AUTO INCREMENT](/docs/server/reference/data-types/auto_increment.md)
* [mysql\_stmt\_insert\_id()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_insert_id.md)
* [LAST\_INSERT\_ID()](/docs/server/reference/sql-functions/secondary-functions/information-functions/last_insert_id.md)

{% @marketo/form formId="4316" %}
