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实现的mysql主从数据库状态检测功能示例

本文实例讲述了PHP实现的mysql主从数据库状态检测功能。分享给大家供大家参考,具体如下: 实例: <?php /** * 检测多个主从数据库是否挂掉 * 建立从...

MySQL的FIND_IN_SET函数使用方法分享

很多时候我们在设计数据库时有这种情况,比如: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是...

php判断输入不超过mysql的varchar字段的长度范围

但是如果在utf-8编码下,一个汉字是占3个字符长度的,比如字符串$str=”你好啊!!”; 如果你用strlen函数来判断,长度是11,正好超过了varchar的长度,但实际上确不是这...

php mysql实现mysql_select_db选择数据库

mysql_select_db介绍 mysql_select_db函数有两个参数: mysql_select_db(database,connection) 如果成功,则该函数返回...

PHP操作MySQL的mysql_fetch_* 函数的常见用法教程

mysql_fetch_* 列函数 mysql_fetch_* 列函数的主要功能是从查询返回的结果集中取得相关的查询结果,主要包括: mysql_fetch_array():从结果...