php mysql获取表字段名称和字段信息的三种方法
程序员文章站
2024-04-02 08:55:34
php mysql获取表字段名称和字段信息的三种方法
先给出本实例中使用的表的信息:
使用desc获取表字段信息
php代码如下:
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 )
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: Java编程探索之泛型擦除实例解析