首页 > php与数据库 > PHP4 与 MySQL 数据库操作函数详解(3)

PHP4 与 MySQL 数据库操作函数详解(3)

7) 数据库信息函数(2个): 

1、mysql_fetch_field() 

格式:object mysql_fetch_field(int query , int [field_offset]); 

返回1个对象,即一哈希表,下标有: 

table : 表名 

name : 字段名 

max_length : 该字段的最大长度 

not_null : 字段为not null则返回1,否则返回0 

primary_key : 字段为primary key则返回1,否则返回0 

unique_key : 字段为unique key则返回1,否则返回0 

multiple_key : 字段为非unique key则返回1,否则返回0 

numeric : 字段为numeric则返回1,否则返回0 

blob : 字段为blob则返回1,否则返回0 

type : 字段的类型 

unsigned : 字段为unsigned则返回1,否则返回0 

zerofill : 字段为zero filled则返回1,否则返回0 

引用格式为:对象名->下标名 

使用此函数可以得到表名、字段名、类型....... 

例子:

<?php
$query = mysql_query($sql , $connect); 
while($object = mysql_fetch_field($query)) 
{ 
echo "table name : ".$object->table.""; 
echo "field name : ".$object->name.""; 
echo "primary key : ".$object->primary_key.""; 
echo "not null : ".$object->not_null.""; 
echo "field type : ".$object->type.""; 
echo "field max length : ".$object->max_length.""; 
} 
?>

Note : 哈希表的是从0坐标开始的,即第一个字段为哈希表中的0项。

如果我们想直接得到哈希表的第三项即第三个字段的信息,可用如下格式:

<?php
$query = mysql_query($sql , $connect); 
$object = mysql_fetch_field($query , 2); 
echo "table name : ".$object->table.""; 
echo "field name : ".$object->name.""; 
echo "primary key : ".$object->primary_key.""; 
echo "not null : ".$object->not_null.""; 
echo "field type : ".$object->type.""; 
echo "field max length : ".$object->max_length."";


本文地址:http://www.phprm.com/database/3c2cb1f3c7ef650b1c955c7bcbcce6df.html

转载随意,但请附上文章地址:-)

标签:none

发表留言