Current Location: Home> Function Categories> mysqli_result::fetch_field_direct

mysqli_result::fetch_field_direct

(mysqli_fetch_field_direct) Get metadata for a single field
Name:mysqli_result::fetch_field_direct
Category:MySQLi
Programming Language:php
One-line Description:Returns metadata for a single field in the result set as an object.

Definition and usage

fetch_field_direct() / mysqli_fetch_field_direct() function returns the metadata of a single field in the result set as an object.

Example

Example 1 - Object-Oriented Style

Returns the metadata of a single field in the result set, and prints the field's name, table, and maximum length:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;

if ( $result = $mysqli -> query ( $sql ) ) {

  // Get the field information of column "Age"
  $fieldinfo = $result -> fetch_field_direct ( 1 ) ;

  printf ( "Name: %s\n" , $fieldinfo -> name ) ;
  printf ( "Table: %s\n" , $fieldinfo -> table ) ;
  printf ( "Max. Len: %d\n" , $fieldinfo -> max_length ) ;
  $result -> free_result ( ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns the metadata of a single field in the result set, and prints the field's name, table, and maximum length:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;

if ( $result = mysqli_query ( $con , $sql ) ) {

  // Get the field information of column "Age"
  $fieldinfo = mysqli_fetch_field_direct ( $result , 1 ) ;

  printf ( "Name: %s\n" , $fieldinfo -> name ) ;
  printf ( "Table: %s\n" , $fieldinfo -> table ) ;
  printf ( "Max. Len: %d\n" , $fieldinfo -> max_length ) ;

  mysqli_free_result ( $result ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles