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

mysqli_result::fetch_fields

(mysqli_fetch_fields) Returns an array of objects representing fields in the result set
Name:mysqli_result::fetch_fields
Category:MySQLi
Programming Language:php
One-line Description:Returns an array of objects representing fields in the result set.

Definition and usage

fetch_fields() / mysqli_fetch_fields() functions return an array of objects representing fields in the result set.

Example

Example 1 - Object-Oriented Style

Returns an array of objects that represent fields in the result set, and then prints the name, table, and maximum length of each field:

 <?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 field information for all fields
  $fieldinfo = $result -> fetch_fields ( ) ;

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

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns an array of objects that represent fields in the result set, and then prints the name, table, and maximum length of each field:

 <?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 field information for all fields
  $fieldinfo = mysqli_fetch_fields ( $result ) ;

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

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