gpt4 book ai didi

php mysql获取表字段名称和字段信息的三种方法

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php mysql获取表字段名称和字段信息的三种方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

php mysql获取表字段名称和字段信息的三种方法 。

先给出本实例中使用的表的信息:

php mysql获取表字段名称和字段信息的三种方法

使用desc获取表字段信息 。

php代码如下:

?
1
2
3
4
5
6
7
8
9
<?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 );
   }
?>

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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代码如下:

?
1
2
3
4
5
6
7
8
9
<?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 );
   }
?>

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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 );
   }
?>

运行结果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 mysql获取表字段名称和字段信息的三种方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com