MySQL Reference Manual for version 4.0.18.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.1.6 C API Prepared Statement Function Overview

Note: The API for prepared statements is still subject to revision. This information is provided for early adopters, but please be aware that the API may change.

The functions available for prepared statement processing are summarized here and described in greater detail in a later section. See section 19.1.7 C API Prepared Statement Function Descriptions.

Associates application data buffers with the parameter markers in a prepared SQL statement. Associates application data buffers with columns in the result set. Executes the prepared statement. Fetches the next row of data from the result set and returns data for all bound columns. Fetch data for one column of the current row of the result set. Returns prepared statement metadata in the form of a result set. Returns the number of parameters in a prepared SQL statement. Return parameter metadata in the form of a result set. Prepares an SQL string for execution. Sends long data in chunks to server. Returns the number of rows changes, deleted, or inserted by the last UPDATE, DELETE, or INSERT query. Frees memory used by prepared statement. Seeks to an arbitrary row number in a statement result set. Returns the error number for the last statement execution. Returns the error message for the last statement execution. Free the resources allocated to the statement handle. Returns total rows from the statement buffered result set. Reset the statement buffers in the server. Seeks to a row offset in a statement result set, using value returned from mysql_stmt_row_tell(). Returns the statement row cursor position. Returns the SQLSTATE error code for the last statement execution. Retrieves the complete result set to the client.
Function Description
mysql_bind_param()
mysql_bind_result()
mysql_execute()
mysql_fetch()
mysql_fetch_column()
mysql_get_metadata()
mysql_param_count()
mysql_param_result()
mysql_prepare()
mysql_send_long_data()
mysql_stmt_affected_rows()
mysql_stmt_close()
mysql_stmt_data_seek()
mysql_stmt_errno()
mysql_stmt_error()
mysql_stmt_free_result()
mysql_stmt_num_rows()
mysql_stmt_reset()
mysql_stmt_row_seek()
mysql_stmt_row_tell()
mysql_stmt_sqlstate()
mysql_stmt_store_result()

Call mysql_prepare() to prepare and initialize the statement handle, mysql_bind_param() to supply the parameter data, and mysql_execute() to execute the query. You can repeat the mysql_execute() by changing parameter values in the respective buffers supplied through mysql_bind_param().

If the query is a SELECT statement or any other query that produces a result set, mysql_prepare() will also return the result set metadata information in the form of a MYSQL_RES result set through mysql_get_metadata().

You can supply the result buffers using mysql_bind_result(), so that the mysql_fetch() will automatically return data to these buffers. This is row-by-row fetching.

You can also send the text or binary data in chunks to server using mysql_send_long_data(), by specifying the option is_long_data=1 or length=MYSQL_LONG_DATA or -2 in the MYSQL_BIND structure supplied with mysql_bind_param().

When statement execution has been completed, the statement handle must be closed using mysql_stmt_close() so that all resources associated with it can be freed.

If you obtained a SELECT statement's result set metadata by calling mysql_get_metadata(), you should also free it using mysql_free_result().

Execution Steps

To prepare and execute a statement, an application follows these steps:

  1. Call mysql_prepare() and pass it a string containing the SQL statement. For a successful prepare operation, mysql_prepare() returns a valid statement handle to the application.
  2. If the query produces a result set, call mysql_get_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_bind_param(). All parameters must be set. Otherwise, query execution will return an error or produce unexpected results.
  4. Call mysql_execute() to execute the statement.
  5. If the query produces a result set, bind the data buffers to use for retrieving the row values by calling mysql_bind_result().
  6. Fetch the data into the buffers row by row by calling mysql_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_prepare() is called, the MySQL client/server protocol performs these actions:

When mysql_execute() is called, the MySQL client/server protocol performs these actions:

When mysql_fetch() is called, the MySQL client/server protocol performs these actions:

You can get the statement error code, error message, and SQLSTATE value using mysql_stmt_errno(), mysql_stmt_error(), and mysql_stmt_sqlstate(), respectively.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by rdg (Feb 25 2004) using texi2html