Skip to main content

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

PropertyTypeDescription
fieldCountNumberDefaults to 0 for non-SELECT queries.
affectedRowsNumberThe number of rows affected by the query (does not necessarily mean all rows were changed).
insertIdNumberThe ID of the last inserted row (if an auto-increment column exists, otherwise 0).
serverStatusNumberA bitmask flag representing the current state of the MySQL server.
warningCountNumberThe number of warnings generated during query execution.
messageStringAn optional message providing additional information about the query result (typically empty).
protocol41Booleantrue if MySQL protocol 4.1 or later is used.
changedRowsNumberThe 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-ValueStatusDescription
1SERVER_STATUS_AUTOCOMMITThe server is in autocommit mode, treating each query as a seperate transaction that commits automatically.
2SERVER_STATUS_IN_TRANSThe server is currently in a transaction (queries have not been committed yet).
8SERVER_STATUS_MORE_RESULTS_EXISTSMore result sets exist (e.g., when executing a SELECT ... INTO OUTFILE query).
16SERVER_STATUS_NO_GOOD_INDEX_USEDThe server did not use an optimal index for the query, which may indicate inefficient execution.
32SERVER_STATUS_NO_INDEX_USEDThe query was executed without an index, likely causing a full table scan (may impact performance).
64SERVER_STATUS_QUERY_NO_GOOD_INDEX_USEDNo suitable index was used during query execution (could result in performance issues).
128SERVER_STATUS_CURSOR_EXISTSA cursor exists and is in use for this query (typically seen in more complex queries).
256SERVER_STATUS_LAST_INSERT_IDThe last executed INSERT statement generated an AUTO_INCREMENT ID.
512SERVER_STATUS_DB_DROPPEDA database was dropped as part of the executed query.