SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP MySQL Functions to get details of the fields of a table

We can get all the details about the fields used in a table by using the mysql_fetch_field() function. This function returns an object and using that we can get required information about the field. Here in the query we have to specify the fields for which the detail information is required. We also can use symbol * to get information on all the fields of the table. If we are mentioning field names then we will only get details of those fields only.

Here we will use mysql_num_fields() to get the number of fields there in the table. Here is the code to display details of the fields.

<?
$query="select * from student";
$result=mysql_query($query) or die( "query failed");

$i = 0;
while ($i < mysql_num_fields ($result)) {
$row = mysql_fetch_field ($result);

echo "<PRE>
Information for column Name:<b>$row->name</b>
type: $row->type
max_length: $row->max_length
multiple_key: $row->multiple_key
name: $row->name
not_null: $row->not_null
numeric: $row->numeric
primary_key: $row->primary_key
table: $row->table
unique_key: $row->unique_key
unsigned: $row->unsigned
zerofill: $row->zerofill
blob: $row->blob
</PRE>";
$i++;
}
mysql_free_result ($result);
?>



The out put of above code is given below for one field only, but same way for other fields also we can get the result.

Information for column Name:name
type: string
max_length: 11
multiple_key: 0
name: name
not_null: 1
numeric: 0
primary_key: 0
table: student
unique_key: 0
unsigned: 0
zerofill: 0
blob: 0




Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
SQL Tutorial List
SQL site Map