23.8.7.22 mysql_field_count()
unsigned int mysql_field_count(MYSQL *mysql)
설명
연결에서 최근 쿼리의 컬럼의 수를 돌려줍니다.
이 함수를 일반적으로 사용하는 경우에는 mysql_store_result() 가 NULL 을 반환 (및 따라서 결과 세트 포인터가없는) 경우입니다. 이 경우 mysql_field_count() 를 호출하여 mysql_store_result() 가 비어 있지 않은 결과를 생성하는지 여부를 확인할 수 있습니다. 이를 통해 클라이언트 프로그램은 쿼리가 SELECT (또는 SELECT 비슷) 문 이었는지 여부를 몰라도 올바른 조치를 취할 수 있습니다. 이 예시에서는이를 실행하는 방법을 설명하고 있습니다.
섹션 23.8.15.1 "mysql_query ()가 성공을 반환 한 후 mysql_store_result ()가 NULL을 반환 할 수있는 이유는 무엇인가" 를 참조하십시오.
반환 값
결과 집합의 열 수를 나타내는 부호없는 정수.
오류
없음.
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_field_count() 의 값에서 문이 SELECT 였는지 여부를 추정하는 것이 아니라, mysql_store_result() 에서 직접 오류를 확인합니다.