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

作者:lqh 时间:2023-11-18 22:47:26 

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

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

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,mysql表字段
0
投稿

猜你喜欢

  • python正则-re的用法详解

    2022-05-19 07:50:15
  • 解决python 打包成exe太大的问题

    2021-09-22 11:33:59
  • Python中使用Inotify监控文件实例

    2021-03-03 14:17:05
  • Python入门教程(三十三)Python的字符串格式化

    2023-04-03 11:58:17
  • MySQL 8.0 redo log的深入解析

    2024-01-12 23:44:22
  • python 遗传算法求函数极值的实现代码

    2023-08-29 11:36:11
  • 卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法

    2011-11-03 16:49:09
  • windows开发记事本程序纪实(二)逻辑篇1

    2022-05-15 10:52:51
  • python中实现定制类的特殊方法总结

    2023-06-19 10:32:31
  • Python中 join() 函数的使用示例讲解

    2023-03-29 02:32:25
  • Python编程使用matplotlib挑钻石seaborn画图入门教程

    2021-04-12 18:28:17
  • 数据库连接方式汇总

    2024-01-15 22:15:06
  • Python数据分析pandas模块用法实例详解

    2023-01-25 00:47:29
  • 让innerText在firefox火狐和IE浏览器都能用的写法

    2024-05-02 16:17:24
  • SQL server 定时自动备份数据库的图文方法

    2024-01-14 17:55:50
  • Linux下远程连接Jupyter+pyspark部署教程

    2021-03-31 18:35:59
  • ASP 统计某字符串中“A”出现过的次数

    2010-08-12 10:17:00
  • pandas loc iloc ix用法详细分析

    2021-08-31 20:16:53
  • MySql中sql语句执行过程详细讲解

    2024-01-18 07:44:26
  • Python要求O(n)复杂度求无序列表中第K的大元素实例

    2023-07-30 13:18:01
  • asp之家 网络编程 m.aspxhome.com