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


Posted in PHP onNovember 13, 2016

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 相关文章推荐
用ODBC的分页显示
Oct 09 PHP
php 字符转义 注意事项
May 27 PHP
PHP simple_html_dom.php+正则 采集文章代码
Dec 24 PHP
IIS6.0中配置php服务全过程解析
Aug 07 PHP
php header函数的常用http头设置
Jun 25 PHP
简单谈谈PHP中strlen 函数
Feb 27 PHP
Thinkphp单字母函数使用指南
May 08 PHP
项目中应用Redis+Php的场景
May 22 PHP
PHP命名空间namespace的定义方法详解
Mar 29 PHP
PHP空值检测函数与方法汇总
Nov 19 PHP
laravel使用数据库测试注意事项
Apr 10 PHP
PHP实现简易用户登录系统
Jul 10 PHP
PHP编写daemon process 实例详解
Nov 13 #PHP
php版微信小店API二次开发及使用示例
Nov 12 #PHP
PHP Mysqli 常用代码集合
Nov 12 #PHP
PHP版微信小店接口开发实例
Nov 12 #PHP
PHP错误和异常处理功能模块示例
Nov 12 #PHP
php版微信小店调用api示例代码
Nov 12 #PHP
php实用代码片段整理
Nov 12 #PHP
You might like
用PHP实现Ftp用户的在线管理的代码
2007/03/06 PHP
snoopy 强大的PHP采集类使用实例代码
2010/12/09 PHP
WordPress迁移时一些常见问题的解决方法整理
2015/11/24 PHP
win7安装php框架Yii的方法
2016/01/25 PHP
PHP实现留言板功能的详细代码
2017/03/25 PHP
tp5框架基于Ajax实现列表无刷新排序功能示例
2020/02/10 PHP
MooTools 页面滚动浮动层智能定位实现代码
2011/08/23 Javascript
javascript自动生成包含数字与字符的随机字符串
2015/02/09 Javascript
jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]
2017/07/05 jQuery
Angularjs之ngModel中的值验证绑定方法
2018/09/13 Javascript
深入理解 Koa 框架中间件原理
2018/10/18 Javascript
python操作MongoDB基础知识
2013/11/01 Python
python中的函数用法入门教程
2014/09/02 Python
Python Web框架Flask中使用新浪SAE云存储实例
2015/02/08 Python
Python自定义scrapy中间模块避免重复采集的方法
2015/04/07 Python
总结Python编程中函数的使用要点
2016/03/20 Python
python opencv之SIFT算法示例
2018/02/24 Python
Python学习笔记基本数据结构之序列类型list tuple range用法分析
2019/06/08 Python
python设计tcp数据包协议类的例子
2019/07/23 Python
python redis 批量设置过期key过程解析
2019/11/26 Python
Python 生成一个从0到n个数字的列表4种方法小结
2019/11/28 Python
python opencv 实现读取、显示、写入图像的方法
2020/06/08 Python
如何用Python 实现全连接神经网络(Multi-layer Perceptron)
2020/10/15 Python
python matlab库简单用法讲解
2020/12/31 Python
使用css3实现的windows8开机加载动画
2014/12/09 HTML / CSS
美国百年历史早餐食品供应商:Wolferman’s
2017/01/18 全球购物
AC Lens:购买隐形眼镜
2017/02/26 全球购物
美国首屈一指的高品质珠宝设计师和零售商:Allurez
2018/01/23 全球购物
初级软件工程师面试题 Junior Software Engineer Interview
2015/02/15 面试题
会计电算一体化个人简历的自我评价
2013/10/15 职场文书
旅游业大学生创业计划书
2014/01/31 职场文书
劳资协议书范本
2014/04/23 职场文书
入党宣誓大会后的感想
2015/08/10 职场文书
2019初中学生入团申请书
2019/06/27 职场文书
浅谈TypeScript 索引签名的理解
2021/10/16 Javascript
Python tensorflow卷积神经Inception V3网络结构
2022/05/06 Python