You can call the mysql_num_fields() function to return the number of columns in a result set.
Syntax
unsigned int
mysql_num_fields(MYSQL_RES *result)
Note
To pass a MYSQL* argument, use unsigned int mysql_field_count(MYSQL *mysql).
Return values
An unsigned integer representing the number of columns in a result set is returned.
Errors
None.
Notes
You can obtain the number of columns from a pointer to a result set or to a connection handler. If mysql_store_result() or mysql_use_result() returns NULL, no result set pointer exists. You can use the connection handler. 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 actions without knowing whether the query was a SELECT (or SELECT-like) statement.