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. Her is the code to display details of the fields.




Related Tutorial
Printing Error message
Posting error message to an email address
PHP MySQL data display
<?
$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 filed 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

Discuss this tutorial at forum

List of SQL Tutorials


Scripts
PHP
JavaScript
SQL Tutorial List
SQL (Home)
mysql_affected_rows mysql_change_user mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_query mysql_result mysql_select_db mysql_tablename
SQL site Map
Knowledge Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.