php mysql获取表字段名称和字段信息的三种方法
作者:lqh 发布时间:2023-11-18 22:47:26
标签: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
)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


猜你喜欢
- 程序图标主要作用是为了使该程序更加具象及更容易理解,除了上述的作用外,有更好视觉效果的图标可以提高产品的整体体验和品牌,可引起用户的关注和下
- MySQL 拥有一个复杂的但直观易学的 SQL 接口。这个章节描述了各种不同的命令、类型和函数,为了高效地使用 MySQL 需要了解它们。这
- 这篇文章主要是用PHP函数实现数字与文字分页,具体实现步骤就不罗嗦了,直接上代码/** * * @param $_sql * @param
- 本人非计算机,亦非心理学,或者交互设计,更非设计专业出身,因此什么都是半桶水。即使如此,依然靠着兴趣寻找乐趣。对于设计,爱之,但没有受过系统
- 1、最郁闷的发现!!先看代码:<style>#a #b #c span{color:red;}#b #c span{color:
- 前言daemon 音标 : [‘di:mən] , 中文含义为守护神或精灵的意思 . 其实它还有个意思 : 守护进程 .Daemon程序是一
- 1.包: package PaintBrush; /** * * @author lucifer */ public class Paint
- 在照着Tensorflow官网的demo敲了一遍分类器项目的代码后,运行倒是成功了,结果也不错。但是最终还是要训练自己的数据,所以尝试准备加
- 很多网站现在都有使用QQ作为在线客服工具,我们点击它可以很方便的和网站人员联系,本站为你整理了在网站上使用QQ在线客服的代码,共13种风格,
- 呃,看到这个标题,我们可以首先将IE系浏览器无视了。我承认,我是有极简主义倾向的,我希望能够使用最少的代码和图片做更多的事情。虽然CSS3仅
- 一、安装环境gym是用于开发和比较强化学习算法的工具包,在python中安装gym库和其中子场景都较为简便。安装gym:pip instal
- 近日大家热议的盗版XP黑屏问题想必很多人都知道了,在这里就不多说。据媒体报道,微软公司将实施逐步投放策略,预计本周将投放5%黑屏,下周投放2
- 本程序有两文件test.asp 和tree.asp 还有一些图标文件 1。test.asp 调用类生成树 代码如下<%@
- asp取得字段属性代码:set AdoX = server.createobject("adox.c
- 本文介绍如何建立基于Web的日历,同时为不熟悉Active Server Pages(ASP)、SQL和ADO的开发者提供建立Web站点的过
- 协程协程(co-routine,又称微线程)是一种多方协同的工作方式。当前执行者在某个时刻主动让出(yield)控制流,并记住自身当前的状态
- Harris 角点检测算法1. 角点角点是水平方向、垂直方向变化都很大的像素。角点检测算法的基本思想:
- 作者:bencalie 整理日期:2004年6月15日<xml id="users"> <u
- 导言:在前面的教程里我们探讨了如何为GridView控件添加radio buttons列。当用户最多只能选择一项数据时,我们可以在用户界面里
- 现在的垃圾留言越来越智能,并且从留言内容几乎看不出来是垃圾留言,而大量的垃圾留言会导致文章可读性下降,并可能会被搜索引擎惩罚,经过一段时间的