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

yipeiwu_com4年前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在DOS命令行下却无法链接MySQL的技术笔记

正好今天朋友 xjb 也碰到了这个问题,所以写了这篇笔记,将此问题的描述以及解决记录下。 问题描述:用 web 方式, 可以链接 mysql, 但是在命令行下, 却提示:   Fatal...

PHP与MySQL开发中页面出现乱码的一种解决方法

一般来说,乱码的出现有2种原因,首先是由于编码(charset)设置错误,导致浏览器以错误的编码来解析,从而出现了满屏乱七八糟的“天书”,其次是文件被以错误的编码打开,然后保存,比如一个...

php实现mysql备份恢复分卷处理的方法

本文实例讲述了php实现mysql备份恢复分卷处理的方法。分享给大家供大家参考。具体分析如下: 分卷处理就是把握们要处理的数据分成一个个小文件进行处理了,这里我来给大家介绍一个php m...

Windows下PHP开发环境搭建教程(Apache+PHP+MySQL)

Windows下PHP开发环境搭建教程(Apache+PHP+MySQL)

由于换电脑或重装系统后常需要重新搭建PHP环境,此次简单记录一下Windows下搭建PHP环境的过程,具体步骤可以参照网上资料 准备工作: Windows下手工搭建PHP环境需要先下载相...

PHP5中使用mysqli的prepare操作数据库的介绍

php5中有了mysqli对prepare的支持,对于大访问量的网站是很有好处的,极大地降低了系统开销,而且保证了创建查询的稳定性和安全性。 PHP5.0后我们可以使用mysqli,my...