MySQL :: MySQL 8.4 C API Developer Guide :: 5.4.22 mysql_fetch_row() (original) (raw)
5.4.22 mysql_fetch_row()
MYSQL_ROW
mysql_fetch_row(MYSQL_RES *result)
Description
Note
mysql_fetch_row() is a synchronous function. Its asynchronous counterpart ismysql_fetch_row_nonblocking(), for use by applications that require asynchronous communication with the server. SeeChapter 7, C API Asynchronous Interface.
mysql_fetch_row() retrieves the next row of a result set:
- When used aftermysql_store_result() ormysql_store_result_nonblocking(),mysql_fetch_row() returns
NULL
if there are no more rows to retrieve. - When used aftermysql_use_result(),mysql_fetch_row() returns
NULL
if there are no more rows to retrieve or an error occurred.
The number of values in the row is given bymysql_num_fields(result). Ifrow
holds the return value from a call tomysql_fetch_row(), pointers to the values are accessed as row[0]
torow[mysql_num_fields(result)-1]
.NULL
values in the row are indicated byNULL
pointers.
The lengths of the field values in the row may be obtained by calling mysql_fetch_lengths(). Empty fields and fields containing NULL
both have length 0; you can distinguish these by checking the pointer for the field value. If the pointer isNULL
, the field is NULL
; otherwise, the field is empty.
Return Values
A MYSQL_ROW
structure for the next row, orNULL
. The meaning of aNULL
return depends on which function was called precedingmysql_fetch_row():
- When used aftermysql_store_result() ormysql_store_result_nonblocking(),mysql_fetch_row() returns
NULL
if there are no more rows to retrieve. - When used aftermysql_use_result(),mysql_fetch_row() returns
NULL
if there are no more rows to retrieve or an error occurred. To determine whether an error occurred, check whethermysql_error() returns a nonempty string ormysql_errno() returns nonzero.
Errors
Errors are not reset between calls tomysql_fetch_row()
- CR_SERVER_LOST
The connection to the server was lost during the query. - CR_UNKNOWN_ERROR
An unknown error occurred.
Example
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for(i = 0; i < num_fields; i++)
{
printf("[%.*s] ", (int) lengths[i],
row[i] ? row[i] : "NULL");
}
printf("\n");
}