PHP mysql에서 테이블 필드 이름과 필드 정보를 얻는 세 가지 방법

黄舟
풀어 주다: 2023-03-06 08:14:02
원래의
2859명이 탐색했습니다.

PHP mysql이 테이블 필드 이름과 필드 정보를 얻는 세 가지 방법

먼저 이 예제에 사용된 테이블에 대한 정보를 제공합니다.

PHP mysql에서 테이블 필드 이름과 필드 정보를 얻는 세 가지 방법

desc를 사용하여 테이블 필드 정보 가져오기

php 코드는 다음과 같습니다.

<?php 
  mysql_connect("localhost","root","");
  mysql_select_db("test");
  $query = "desc student";
  $result = mysql_query($query);
  while($row=mysql_fetch_assoc($result)){
 print_r($row);
  }
?>
로그인 후 복사



실행 결과:

Array
(
  [Field] => student_id
  [Type] => int(4)
  [Null] => NO
  [Key] => PRI
  [Default] => 
  [Extra] => auto_increment
)
Array
(
  [Field] => student_name
  [Type] => varchar(50)
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
)
Array
(
  [Field] => class_id
  [Type] => int(4)
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
)
Array
(
  [Field] => total_score
  [Type] => int(4)
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
)
로그인 후 복사



SHOW FULL FIELDS를 사용하여 테이블 필드 정보 가져오기

php 코드는 다음과 같습니다.

<?php 
  mysql_connect("localhost","root","");
  mysql_select_db("test");
  $query = "SHOW FULL COLUMNS FROM student";
  $result = mysql_query($query);
  while($row=mysql_fetch_assoc($result)){
 print_r($row);
  }
?>
로그인 후 복사



실행 결과:

Array
(
  [Field] => student_id
  [Type] => int(4)
  [Collation] => 
  [Null] => NO
  [Key] => PRI
  [Default] => 
  [Extra] => auto_increment
  [Privileges] => select,insert,update,references
  [Comment] => 
)
Array
(
  [Field] => student_name
  [Type] => varchar(50)
  [Collation] => latin1_swedish_ci
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
  [Privileges] => select,insert,update,references
  [Comment] => 
)
Array
(
  [Field] => class_id
  [Type] => int(4)
  [Collation] => 
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
  [Privileges] => select,insert,update,references
  [Comment] => 
)
Array
(
  [Field] => total_score
  [Type] => int(4)
  [Collation] => 
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
  [Privileges] => select,insert,update,references
  [Comment] => 
)
로그인 후 복사



mysql_fetch_field 메소드를 사용하여 가져오기 테이블 필드 정보

php 코드는

<?php
  mysql_connect("localhost","root","");
  mysql_select_db("test");
  $query = "SELECT * FROM student LIMIT 1";
  $result = mysql_query($query);
  $fields = mysql_num_fields($result);
  for($count=0;$count<$fields;$count++)
  {
   $field = mysql_fetch_field($result,$count);
  print_r($field);
  }
?>
로그인 후 복사



실행 결과는

stdClass Object
(
  [name] => student_id
  [table] => student
  [def] => 
  [max_length] => 1
  [not_null] => 1
  [primary_key] => 1
  [multiple_key] => 0
  [unique_key] => 0
  [numeric] => 1
  [blob] => 0
  [type] => int
  [unsigned] => 0
  [zerofill] => 0
)
stdClass Object
(
  [name] => student_name
  [table] => student
  [def] => 
  [max_length] => 5
  [not_null] => 1
  [primary_key] => 0
  [multiple_key] => 0
  [unique_key] => 0
  [numeric] => 0
  [blob] => 0
  [type] => string
  [unsigned] => 0
  [zerofill] => 0
)
stdClass Object
(
  [name] => class_id
  [table] => student
  [def] => 
  [max_length] => 1
  [not_null] => 1
  [primary_key] => 0
  [multiple_key] => 0
  [unique_key] => 0
  [numeric] => 1
  [blob] => 0
  [type] => int
  [unsigned] => 0
  [zerofill] => 0
)
stdClass Object
(
  [name] => total_score
  [table] => student
  [def] => 
  [max_length] => 3
  [not_null] => 1
  [primary_key] => 0
  [multiple_key] => 0
  [unique_key] => 0
  [numeric] => 1
  [blob] => 0
  [type] => int
  [unsigned] => 0
  [zerofill] => 0
)
로그인 후 복사

위는 테이블 필드명과 필드 정보를 얻기 위한 php mysql의 세 가지 방법입니다. 방법 내용, 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!



원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!