> 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/server/reference/clientserver-protocol/3-binary-protocol-prepared-statements/com_stmt_prepare.md).

# COM\_STMT\_PREPARE

Prepares a statement on the server.

{% hint style="info" %}
Not all statements can be prepared. See [PREPARE](/docs/server/reference/sql-statements/prepared-statements/prepare-statement.md#permitted-statements) for a list of statements that can be prepared.
{% endhint %}

## Implemented by

* [mariadb\_stmt\_execute\_direct()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mariadb_stmt_execute_direct.md)
* [mysql\_stmt\_prepare()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_prepare.md)

## Fields

* [int<1>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) `0x16` `COM_STMT_PREPARE` header
* [string\<EOF>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#end-of-file-length-strings) SQL Statement

## Example

```
1F 00 00 00 16 53 45 4C 45 43 54 20 2A 20 46 52  .....SELECT * FR
1F 4D 20 74 65 73 74 5F 62 69 6E 64 5F 72 65 73  OM test_bind_res
75 6C 74                                         ult
```

## Response

If something goes wrong, the server sends an [ERR\_Packet](/docs/server/reference/clientserver-protocol/4-server-response-packets/err_packet.md). If the command succeeds, different packets are received:

* [COM\_STMT\_PREPARE\_OK](#COM_STMT_PREPARE_OK).
* If number of parameters (count of `?` placeholders) > `0`:
  * For each parameter:
    * [column definition packet](/docs/server/reference/clientserver-protocol/4-server-response-packets/result-set-packets.md).
  * If !`DEPRECATE_EOF` [eof\_packet](/docs/server/reference/clientserver-protocol/4-server-response-packets/eof_packet.md).
* If prepared statement returns result set and number of result set columns > `0`:
  * For each column:
    * [column definition packet](/docs/server/reference/clientserver-protocol/4-server-response-packets/result-set-packets.md).
  * If !`DEPRECATE_EOF` [eof\_packet](/docs/server/reference/clientserver-protocol/4-server-response-packets/eof_packet.md).

### COM\_STMT\_PREPARE\_OK

* [int<1>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) `0x00` `COM_STMT_PREPARE_OK` header.
* [int<4>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) statement ID.
* [int<2>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) number of columns in the returned result set (or 0 if statement does not return result set).
* [int<2>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) number of prepared statement parameters ('?' placeholders).
* [string<1>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-strings) -not used-
* [int<2>](/docs/server/reference/clientserver-protocol/protocol-data-types.md#fixed-length-integers) number of warnings.

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mariadb.com/docs/server/reference/clientserver-protocol/3-binary-protocol-prepared-statements/com_stmt_prepare.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
