You are here : Cmysql/mysql.hmysql_field_count

mysql_field_count() - mysql/mysql.h

 Returns the number of columns for the most recent query on the
        connection.The normal use of this function is when
        mysql_store_result() returned
        NULL (and thus you have no result set
        pointer). In this case, you can call
        mysql_field_count() to determine
        whether mysql_store_result()
        should have produced a nonempty result. This enables the client
        program to take proper action without knowing whether the query
        was a SELECT (or
        SELECT-like) statement. The
        example shown here illustrates how this may be done.mysql_store_result()mysql_store_result()NULLmysql_field_count()mysql_field_count()mysql_store_result()mysql_store_result()SELECTSELECTSELECTSELECTSee Section 25.8.15.1, “Why mysql_store_result() Sometimes Returns NULL After mysql_query()
        Returns Success”.Section 25.8.15.1, “Why mysql_store_result() Sometimes Returns NULL After mysql_query()
        Returns Success”


Syntax

unsigned int mysql_field_count(MYSQL *mysql)


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));
        }
    }
}


Output / Return Value

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


Limitations


Alternatives / See Also


Reference