Name

mysql_field_count —

Synopsis

unsigned int mysql_field_count(MYSQL *mysql)

Returns the number of columns contained in a result set. This function is most useful when checking the type of query last executed. If a call to mysql_store_result returns a null pointer for a result set, either the query was a non-SELECT query (such as UPDATE, INSERT, etc.) or there was an error. By calling mysql_field_count, you can determine which was the case, because a non-SELECT query always returns zero fields, and a SELECT query always returns at least one field. Even if the returned query has no rows, the fields of the SELECT query will be reflected here.

Example

MYSQL_FIELD field;
MYSQL_RES *result;
 
// A query has been executed and returned success
result = mysql_store_result(  );
if (! result ) {
     // Ooops, the result pointer is null, either the query was a non-SELECT
     // query or something bad happened!
     if ( mysql_field_count(&mysql) ) {
          // The number of columns queried is greater than zero, it must have
          // been a SELECT query and an error must have occurred.
     } else {
          // Since the number of columns queried is zero, it must have been
          // a non-SELECT query, so all is well...
     }
}

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.