C API Prepared Statement Function Overview
2199
If you obtained a
SELECT
statement's result set metadata by calling
mysql_stmt_result_metadata()
, you should also free the metadata using
mysql_free_result()
.
Execution Steps
To prepare and execute a statement, an application follows these steps:
1. Create a prepared statement handle with
mysql_stmt_init()
. To prepare the statement on the
server, call
mysql_stmt_prepare()
and pass it a string containing the SQL statement.
2. If the statement will produce a result set, call
mysql_stmt_result_metadata()
to obtain the
result set metadata. This metadata is itself in the form of result set, albeit a separate one from the
one that contains the rows returned by the query. The metadata result set indicates how many
columns are in the result and contains information about each column.
3. Set the values of any parameters using
mysql_stmt_bind_param()
. All parameters must be
set. Otherwise, statement execution returns an error or produces unexpected results.
4. Call
mysql_stmt_execute()
to execute the statement.
5. If the statement produces a result set, bind the data buffers to use for retrieving the row values by
calling
mysql_stmt_bind_result()
.
6. Fetch the data into the buffers row by row by calling
mysql_stmt_fetch()
repeatedly until no
more rows are found.
7. Repeat steps 3 through 6 as necessary, by changing the parameter values and re-executing the
statement.
When
mysql_stmt_prepare()
is called, the MySQL client/server protocol performs these actions:
• The server parses the statement and sends the okay status back to the client by assigning a
statement ID. It also sends total number of parameters, a column count, and its metadata if it is a
result set oriented statement. All syntax and semantics of the statement are checked by the server
during this call.
• The client uses this statement ID for the further operations, so that the server can identify the
statement from among its pool of statements.
When
mysql_stmt_execute()
is called, the MySQL client/server protocol performs these actions:
• The client uses the statement handle and sends the parameter data to the server.
• The server identifies the statement using the ID provided by the client, replaces the parameter
markers with the newly supplied data, and executes the statement. If the statement produces a result
set, the server sends the data back to the client. Otherwise, it sends an okay status and the number
of rows changed, deleted, or inserted.
When
mysql_stmt_fetch()
is called, the MySQL client/server protocol performs these actions:
• The client reads the data from the current row of the result set and places it into the application data
buffers by doing the necessary conversions. If the application buffer type is same as that of the field
type returned from the server, the conversions are straightforward.
If an error occurs, you can get the statement error number, error message, and SQLSTATE code using
mysql_stmt_errno()
,
mysql_stmt_error()
, and
mysql_stmt_sqlstate()
, respectively.
Prepared Statement Logging
For prepared statements that are executed with the
mysql_stmt_prepare()
and
mysql_stmt_execute()
C API functions, the server writes
Prepare
and
Execute
lines to the
general query log so that you can tell when statements are prepared and executed.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...