1. 형식
형식1) result= mysql_fetch_field(myres_ptr)
형식2) result= mysql_fetch_field(myres_ptr, field_no)
2. 기능: 인자 myres_ptr의 항목에 대한 정보(이름, 소속 테이블, 초기값, 자료형,
크기, 최대 크기, 플래그, 소수 아래 자리수)를 객체로 얻는다.
3. 설명
(1) 이 함수는 MySQL에서 지원하는 함수이며 함수명은 MySQL의 C API와 동일하다.
(2) 인자 myres_ptr은 MySQL result 포인터이며 mysql_store_result()함수나 기타
결과를 저장하는 함수에 의하여 얻은 값이고 MySQL result 포인터가 아니면
에러 처리되며, 이미 해제된 MySQL result 포인터일 때에는 경고 메시지를
출력하고 result에 -1 의 값을 되돌린다.
(3) 형식1)은 필드 커서 위치, 형식2)는 field_no 위치의 항목 정보를 얻는다.
(4) 형식2)의 인자 field_no는 항목에 대한 순번( 0 부터 항목개수 - 1 )이며 이
값이 문자열이고 수치로 변환될 수 없을 때는 에러 처리된다.
(5) 형식2)의 인자 field_no가 0 보다 작거나 또는 항목의 개수보다 크거나 같으
면 정수 -1 이 result에 돌려진다.
(6) 위의 설명(2)나 설명(5)의 경우가 아니면 설명(7)의 멤버 변수를 지닌 객체가
result에 돌려진다.
(7) 결과값 객체인 result의 멤버변수 설명
==========================================================
객체와 멤버변수명 자료형 설 명
------------------ -------- ------------------------------
result.name (문자열) 항목의 이름
result.table (문자열) 소속 테이블 이름
result.default (문자열) 항목의 초기값
result.type (문자열) 항목의 자료형
result.type_value (정수) 항목의 자료형을 의미하는 수치
result.length (정수) 항목의 크기(bytes)
result.max_length (정수) 항목의 최대 크기(bytes)
result.decimals (정수) 항목의 소수아래 자리수
result.flags (정수) 항목의 플래그
==========================================================
1) result.type_value의 값 설명(MySQL의 버전에 따라 차이가 있을 수 있음)
================================================================
값 자료형 | 값 자료형 | 값 자료형
---- ---------- | ---- ---------- | ---- -----------------------
0 decimal | 8 bigint | 248 set
1 tinyint | 9 mediumint | 249 tinyblob, tinytext
2 smallint | 10 date | 250 mediumblob, mediumtext
3 int | 11 time | 251 longblob, longtext
4 float | 12 datetime | 252 blob, text
5 double | 13 year | 253 varchar
6 null | 14 date | 254 char
7 timestamp | 247 enum |
================================================================
2) result.flags의 설명(MySQL의 버전에 따라 차이가 있을 수 있음)
==========================================================
result.flags의 bit 값 flag의 의미
------------------------------------------ ---------------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 (1) NOT NULL
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 (2) PRIMARY KEY
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 (4) UNIQUE KEY
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 (8) MULTIPLE KEY
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 (16) BLOB FLAG
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 (32) UNSIGNED FLAG
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 (64) ZEROFILL FLAG
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 (128) BINARY FLAG
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 (256) ENUM FLAG
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 (512) PART KEY
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 (1024) GROUP FLAG
==========================================================
4. 사용예#1
mysql= mysql_connect(NULL, myuser, mypass); // localhost를 연결한다.
if(mysql==MYSQL_ERR) { print("MySQL 서버 연결 에러!\n"); return(-1); }
if(mysql_select_db(mysql, "welhelp")==MYSQL_ERR) // welhelp database 선택
{
print("welhelp database 선택 에러!\n"); return(-1);
}
myres= mysql_list_fields(mysql, "addr"); // addr 테이블의 항목 리스트 얻기
if(myres==MYSQL_ERR) { print("addr error!\n"); return; }
fd= mysql_fetch_field(myres, 1); // 두번째 항목에 대한 정보를 얻는다.
printf("%s %s %s [%d] (%s) %lu %lu %u %u\n", fd.name, fd.table, fd.type,
fd.type_value,fd.default,fd.length,fd.max_length,fd.decimals,fd.flags);
mysql_free_result(myres);
mysql_close(mysql);
------------------------------ [ 출력결과 ] ------------------------------
age addr int [3] () 4 0 0 0
5. 사용예#2
mysql= mysql_connect(NULL, myuser, mypass); // localhost를 연결한다.
if(mysql==MYSQL_ERR) { print("MySQL 서버 연결 에러!\n"); return(-1); }
if(mysql_select_db(mysql, "welhelp")==MYSQL_ERR) // welhelp database 선택
{
print("welhelp database 선택 에러!\n"); return(-1);
}
myres= mysql_list_fields(mysql, "addr"); // addr 테이블의 항목 리스트 얻기
if(myres==MYSQL_ERR) { print("addr error!\n"); return; }
mysql_field_seek(myres, 2); // 3번째 항목으로 필드 커서를 이동한다.
fd= mysql_fetch_field(myres); // 필드 커서의 항목에 대한 정보를 얻는다.
printf("%s %s %s [%d] (%s) %lu %lu %u %u\n", fd.name, fd.table, fd.type,
fd.type_value,fd.default,fd.length,fd.max_length,fd.decimals,fd.flags);
mysql_free_result(myres);
mysql_close(mysql);
------------------------------ [ 출력결과 ] ------------------------------
address addr tinytext [252] () 255 0 0 16
6. 사용예#3
mysql= mysql_connect(NULL, myuser, mypass); // localhost를 연결한다.
if(mysql==MYSQL_ERR) { print("MySQL 서버 연결 에러!\n"); return(-1); }
if(mysql_select_db(mysql,"welhelp")==-1) { print("DB Err!"); return(-1); }
myres= mysql_list_fields(mysql, "addr"); // addr 테이블의 항목 리스트 얻기
if(myres==MYSQL_ERR) { print("addr error!\n"); return; }
while( (fd= mysql_fetch_field(myres))!=MYSQL_ERR )
{
printf(" %s %s %s [%d] (%s) %lu %lu %u %u\n", fd.name,fd.table,fd.type,
fd.type_value, fd.default, fd.length, fd.max_length,
fd.decimals, fd.flags);
}
mysql_free_result(myres); mysql_close(mysql);
------------------------------ [ 출력결과 ] ------------------------------
name addr tinytext [252] () 255 0 0 17
age addr int [3] () 4 0 0 0
address addr tinytext [252] () 255 0 0 16
|