23.8.7.47 mysql_num_fields()
unsigned int mysql_num_fields(MYSQL_RES
*result)
대신 MYSQL* 인수를 전달하려면 unsigned int mysql_field_count(MYSQL *mysql) 을 사용합니다.
설명
결과 집합의 열 수를 반환합니다.
결과 집합 또는 연결 핸들 중 하나의 포인터에서 열 수를 얻을 수 있습니다. 연결 핸들은 mysql_store_result() 또는 mysql_use_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_errno(&mysql))
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
else if (mysql_field_count(&mysql) == 0)
{
// query does not return data
// (it was not a SELECT)
num_rows = mysql_affected_rows(&mysql);
}
}
}
대체 방법 (쿼리 결과 집합을 반환해야 이었음을 알고있는 경우)은 mysql_errno(&mysql) 호출을 mysql_field_count(&mysql) 가 0을 반환하는지 여부의 검사를 대체 할 수 있습니다. 이것은 뭔가 이상이있는 경우에만 발생합니다.