Prepared statements transmit data between the client and server using C language variables on the client side that correspond to SQL values on the server side. If there is a mismatch between the C variable type on the client side and the corresponding SQL value type on the server side, MySQL performs implicit type conversions in both directions.
MySQL knows the type code for the SQL value on the server side.
buffer_type value in the
MYSQL_BIND structure indicates the type code
of the C variable that holds the value on the client side. The
two codes together tell MySQL what conversion must be performed,
if any. Here are some examples:
If you use
intvariable to pass an integer value to the server that is to be stored into a
FLOATcolumn, MySQL converts the value to floating-point format before storing it.
If you fetch an SQL
MEDIUMINTcolumn value, but specify a
MYSQL_TYPE_LONGLONGand use a C variable of type
long long intas the destination buffer, MySQL converts the
MEDIUMINTvalue (which requires less than 8 bytes) for storage into the
long long int(an 8-byte variable).
If you fetch a numeric column with a value of 255 into a
charcharacter array and specify a
MYSQL_TYPE_STRING, the resulting value in the array is a 4-byte string
DECIMALvalues as the string representation of the original server-side value, which is why the corresponding C type is
char. For example,
12.345is returned to the client as
'12.345'. If you specify
MYSQL_TYPE_NEWDECIMALand bind a string buffer to the
mysql_stmt_fetch()stores the value in the buffer as a string without conversion. If instead you specify a numeric variable and type code,
mysql_stmt_fetch()converts the string-format
DECIMALvalue to numeric form.
BITvalues are returned into a string buffer, which is why the corresponding C type is
char. The value represents a bit string that requires interpretation on the client side. To return the value as a type that is easier to deal with, you can cause the value to be cast to integer using either of the following types of expressions:
SELECT bit_col + 0 FROM t SELECT CAST(bit_col AS UNSIGNED) FROM t
To retrieve the value, bind an integer variable large enough to hold the value and specify the appropriate corresponding integer type code.
Before binding variables to the
structures that are to be used for fetching column values, you
can check the type codes for each column of the result set. This
might be desirable if you want to determine which variable types
would be best to use to avoid type conversions. To get the type
after executing the prepared statement with
metadata provides access to the type codes for the result set as
described in Section 22.214.171.124, “mysql_stmt_result_metadata()”, and
Section 27.8.5, “C API Data Structures”.
To determine whether output string values in a result set
returned from the server contain binary or nonbinary data, check
charsetnr value of the result set
metadata is 63 (see Section 27.8.5, “C API Data Structures”). If
so, the character set is
indicates binary rather than nonbinary data. This enables you to
VARCHAR, and the
BLOB types from the
If you cause the
max_length member of the
MYSQL_FIELD column metadata structures to be
set (by calling
mysql_stmt_attr_set()), be aware
max_length values for the result set
indicate the lengths of the longest string representation of the
result values, not the lengths of the binary representation.
max_length does not necessarily
correspond to the size of the buffers needed to fetch the values
with the binary protocol used for prepared statements. Choose
the size of the buffers according to the types of the variables
into which you fetch the values. For example, a
TINYINT column containing the value -128
might have a
max_length value of 4. But the
binary representation of any
requires only 1 byte for storage, so you can supply a
signed char variable in which to store the
value and set
is_unsigned to indicate that
values are signed.
Metadata changes to tables or views referred to by prepared statements are detected and cause automatic repreparation of the statement when it is next executed. For more information, see Section 8.10.4, “Caching of Prepared Statements and Stored Programs”.