php获取mysql表字段名称和字段信息的三种方法
时间:2016-07-19
本文章向大家介绍php获取mysql表字段名称和字段信息的三种方法,分别为:mysql_fetch_field方法、desc方法和SHOW FULL FIELDS方法,需要的朋友可以参考一下本文章。
先给出本实例中使用的表的信息:
使用desc获取表字段信息
php代码如下:
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$query = "desc student";
$result = mysql_query($query);
/* http://www.manongjc.com/article/1225.html */
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概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Linux系统交换空间介绍
- 小内存服务器上宝塔默认安装的MySQL如何优化配置
- (译)SDL编程入门(15)旋转和翻转
- linux解决ping通但端口不通的问题
- (译)SDL编程入门(13)透明度混合
- 基于centos宝塔面版的安装Discuz! Q方法
- 如何在Linux中的特定时间运行命令
- composer 安装过程中,提示404错误
- Linux初始化系统盘后重新挂载数据盘方法
- 在页面部分没有发现字符集声明,请增加该声明
- Linux使用Sudo委派权限
- linux实现定时备份mysql数据库的简单方法
- linux确认已经卸载数据盘并可以新建自定义镜像
- 在Linux中查看进程占用的端口号
- Linux里Makefile是什么?它是如何工作的?