MySQL :: MySQL 8.0 C API Developer Guide :: 5.4.23 mysql_field_count() (original) (raw)

The world's most popular open source database

5.4.23 mysql_field_count()

unsigned int
mysql_field_count(MYSQL *mysql)

Return Values

An unsigned integer representing the number of columns in a result set.

Example

MYSQL_RES *result;
unsigned int num_fields;
unsigned int num_rows;

if (mysql_query(&mysql,query_string))
{
    // error
}
else // query succeeded, process any data returned by it
{
    result = mysql_store_result(&mysql);
    if (result)  // there are rows
    {
        num_fields = mysql_num_fields(result);
        // retrieve rows, then call mysql_free_result(result)
    }
    else  // mysql_store_result() returned nothing; should it have?
    {
        if(mysql_field_count(&mysql) == 0)
        {
            // query does not return data
            // (it was not a SELECT)
            num_rows = mysql_affected_rows(&mysql);
        }
        else // mysql_store_result() should have returned data
        {
            fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
        }
    }
}

An alternative is to replace themysql_field_count(&mysql) call withmysql_errno(&mysql). In this case, you are checking directly for an error frommysql_store_result() rather than inferring from the value ofmysql_field_count() whether the statement was a SELECT.