温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用php怎么获取mysql表字段名称和字段信息

发布时间:2021-02-18 14:49:19 阅读:316 作者:Leah 栏目:开发技术
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

这期内容当中小编将会给大家带来有关使用php怎么获取mysql表字段名称和字段信息,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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表字段名称和字段信息了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×