PHP MysqL获取表字段名称和字段信息的三种方法
先给出本实例中使用的表的信息:
使用desc获取表字段信息
rush:PHP;">
运行结果:
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获取表字段信息
rush:PHP;">
运行结果:
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,references
[Comment] =>
)
Array
(
[Field] => class_id
[Type] => int(4)
[Collation] =>
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
[Privileges] => select,references
[Comment] =>
)
Array
(
[Field] => total_score
[Type] => int(4)
[Collation] =>
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
[Privileges] => select,references
[Comment] =>
)
使用MysqL_fetch_field方法获取表字段信息
rush:PHP;">
PHP
MysqL_connect("localhost","");
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);
}
?>
运行结果如下:
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
)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。