php mysql获取表字段名称和字段信息的三种方法
更新时间:2016年11月13日 17:09:51 投稿:lqh
本文章向大家介绍php获取mysql表字段名称和字段信息的三种方法,分别为:mysql_fetch_field方法、desc方法和SHOW FULL FIELDS方法,
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 echo()和print()、require()和include()函数区别说明
简单总结echo()和print()、require()和include()等易混淆函数的区别2010-03-03PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
这篇文章主要介绍了PHP数组操作,结合实例形式分析php针对数组的添加,删除,计算,反转,排序,查找等操作实现技巧,需要的朋友可以参考下2016-12-12thinkphp 字母函数详解T/I/N/D/M/A/R/U
下面小编就为大家带来一篇thinkphp 字母函数详解T/I/N/D/M/A/R/U。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-04-04
最新评论