PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_fetch_field() Function in PHP


Tutorials »Php »

Topic

What is mysql_fetch_field() function in PHP?
How does mysql_fetch_field() works?




Explanation

This mysql_fetch_field() function gets column information from a query result and returns as an object.

Syntax

object mysql_fetch_field ( resource result [, int field_offset])

Returns an object, using that we can get required information about the field.

The properties of the object are:
  • name - column name
  • table - name of the table the column belongs to
  • def - default value of the column
  • max_length - maximum length of the column
  • not_null - 1 if the column cannot be NULL
  • primary_key - 1 if the column is a primary key
  • unique_key - 1 if the column is a unique key
  • multiple_key - 1 if the column is a non-unique key
  • numeric - 1 if the column is numeric
  • blob - 1 if the column is a BLOB
  • type - the type of the column
  • unsigned - 1 if the column is unsigned
  • zerofill - 1 if the column is zero-filled

mysql_fetch_field() retrieves the column information for a field in a query. If you specify an offset to mysql_fetch_field(), the column properties for that field are returned. If you don't specify an offset, the data for the next field in order will be returned.

Example:


<?php

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

//select database
if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "\n";
}

// Simple Select query
$query = "SELECT * FROM my_table";
$res=mysql_query($query,$link);

/* get column metadata */
echo "Information for column 0:\n";
$meta = mysql_fetch_field($res,0);
if (!$meta) {
echo "No information available\n";
}

echo"
    blob:         $meta->blob
    max_length:   $meta->max_length
    multiple_key: $meta->multiple_key
    name:         $meta->name
    not_null:     $meta->not_null
    numeric:      $meta->numeric
    primary_key:  $meta->primary_key
    table:        $meta->table
    type:         $meta->type
    unique_key:   $meta->unique_key
    unsigned:     $meta->unsigned
    zerofill:     $meta->zerofill";

mysql_free_result($res);

?>


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

Result:

Information for column 0:
blob: 0
max_length: 2
multiple_key: 0
name: id
not_null: 1
numeric: 1
primary_key: 1
table: type
type: int
unique_key: 0
unsigned: 0
zerofill: 0

The same code can be altered to dispaly field information of all the fields in the result set returned by mysql_query() function.


<?php

/* get column metadata */
$i = 0;
while ($i < mysql_num_fields($res)) {
echo "Information for column $i:\n";
$meta = mysql_fetch_field($res);
if (!$meta) {
echo "No information available\n";
}

echo"
    blob:         $meta->blob
    max_length:   $meta->max_length
    multiple_key: $meta->multiple_key
    name:         $meta->name
    not_null:     $meta->not_null
    numeric:      $meta->numeric
    primary_key:  $meta->primary_key
    table:        $meta->table
    type:         $meta->type
    unique_key:   $meta->unique_key
    unsigned:     $meta->unsigned
    zerofill:     $meta->zerofill";
$i++;
}

mysql_free_result($res);

?>

Replace the above code in the previous example to display field information of all the fields.

See also: mysql_field_seek().





Other Links

web hosting