OkPacket
Object
The OkPacket is a response object from MySQL that provides metadata about a successfully executed query. It contains details such as the number of affected rows, insert IDs, server status, warnings, and protocol information. This packet is typically returned for INSERT, UPDATE, DELETE, and other non-SELECT statements.
Structure
| Property | Type | Description |
|---|---|---|
fieldCount | Number | Defaults to 0 for non-SELECT queries. |
affectedRows | Number | The number of rows affected by the query (does not necessarily mean all rows were changed). |
insertId | Number | The ID of the last inserted row (if an auto-increment column exists, otherwise 0). |
serverStatus | Number | A bitmask flag representing the current state of the MySQL server. |
warningCount | Number | The number of warnings generated during query execution. |
message | String | An optional message providing additional information about the query result (typically empty). |
protocol41 | Boolean | true if MySQL protocol 4.1 or later is used. |
changedRows | Number | The number of rows actually modified by the query. |
Common serverStatus Flags
Bitmask Behavior
serverStatus is a bitmask, meaning multiple statuses can be active at the same time.
For example:
SERVER_STATUS_AUTOCOMMIT(1) +SERVER_STATUS_IN_TRANS(2)- These combine to
3(1|2=3) through a bitwise OR operation.
serverStatus-Value | Status | Description |
|---|---|---|
| 1 | SERVER_STATUS_AUTOCOMMIT | The server is in autocommit mode, treating each query as a seperate transaction that commits automatically. |
| 2 | SERVER_STATUS_IN_TRANS | The server is currently in a transaction (queries have not been committed yet). |
| 8 | SERVER_STATUS_MORE_RESULTS_EXISTS | More result sets exist (e.g., when executing a SELECT ... INTO OUTFILE query). |
| 16 | SERVER_STATUS_NO_GOOD_INDEX_USED | The server did not use an optimal index for the query, which may indicate inefficient execution. |
| 32 | SERVER_STATUS_NO_INDEX_USED | The query was executed without an index, likely causing a full table scan (may impact performance). |
| 64 | SERVER_STATUS_QUERY_NO_GOOD_INDEX_USED | No suitable index was used during query execution (could result in performance issues). |
| 128 | SERVER_STATUS_CURSOR_EXISTS | A cursor exists and is in use for this query (typically seen in more complex queries). |
| 256 | SERVER_STATUS_LAST_INSERT_ID | The last executed INSERT statement generated an AUTO_INCREMENT ID. |
| 512 | SERVER_STATUS_DB_DROPPED | A database was dropped as part of the executed query. |