27.7.6.22 mysql_field_count()
unsigned int mysql_field_count(MYSQL *mysql)
Description
返回连接上最新查询的列数。
mysql_store_result()返回NULL
(因此没有结果集指针)时,通常使用此函数。在这种情况下,您可以调用mysql_field_count()来确定mysql_store_result()是否应该产生非空结果。这使 Client 端程序可以采取适当的措施,而无需知道查询是否为SELECT(或SELECTlike)语句。此处显示的示例说明了如何完成此操作。
See 第 27.7.20.1 节“为什么 mysql_store_result()有时在 mysql_query()返回成功后返回 NULL”.
Return Values
一个无符号整数,table 示结果集中的列数。
Errors
None.
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));
}
}
}
一种替代方法是将mysql_field_count(&mysql)呼叫替换为mysql_errno(&mysql)。在这种情况下,您将直接从mysql_store_result()检查错误,而不是从mysql_field_count()的值推断该语句是否为SELECT。