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

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

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

f7f8c6f36881da5171a4abc129ea3dc6.png

使用desc获取表字段信息

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

{

$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)!

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值