PHP Function Reference

PHP mysqli_stmt result_metadata() Method



The PHP mysqli_stmt::result_metadata() / mysqli_stmt_result_metadata() function returns result set metadata from a prepared statement if the prepared statement produces a result set. The returned result object can be used to process the meta information such as total number of fields and individual field information.

Note: The result set returned by this function contains only metadata. It does not contain any row results. The rows are obtained by using the statement handle with mysqli_stmt_fetch().

Note: This result set pointer can be passed as an argument to any of the field-based functions that process result set metadata, such as:

Syntax

//Object-oriented style
public mysqli_stmt::result_metadata()

//Procedural style
mysqli_stmt_result_metadata(statement)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().

Return Value

Returns true on success and data has been fetched. Returns false if an error occurred. Returns null if no more rows/data exists or data truncation occurred.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::result_metadata() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

$stmt = $mysqli>prepare("SELECT Name, Age FROM Employee ORDER BY Age");

//executing the SQL statement
$stmt->execute();

//getting resultset for metadata
$result = $stmt->result_metadata();

//getting field information for all 
//columns from metadata result set
while ($finfo = $result->fetch_field()) {

  printf("Name:     %s\n", $finfo->name);
  printf("Table:    %s\n", $finfo->table);
  printf("max. Len: %d\n", $finfo->max_length);
  printf("Flags:    %d\n", $finfo->flags);
  printf("Type:     %d\n\n", $finfo->type);
}

//free the memory associated with the result
$result->close();

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Name:     Name
Table:    Employee
max. Len: 50
Flags:    1
Type:     254

Name:     Age
Table:    Employee
max. Len: 10
Flags:    32769
Type:     4

Example: Procedural style

The example below shows the usage of mysqli_stmt_result_metadata() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

$stmt = mysqli_prepare($mysqli, "SELECT Name, Age FROM Employee ORDER BY Age");

//executing the SQL statement
mysqli_stmt_execute($stmt);

//getting resultset for metadata
$result = mysqli_stmt_result_metadata($stmt);

//getting field information for all 
//columns from metadata result set
while ($finfo = mysqli_fetch_field($result)) {

  printf("Name:     %s\n", $finfo->name);
  printf("Table:    %s\n", $finfo->table);
  printf("max. Len: %d\n", $finfo->max_length);
  printf("Flags:    %d\n", $finfo->flags);
  printf("Type:     %d\n\n", $finfo->type);
}

//free the memory associated with the result
mysqli_free_result($result);

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Name:     Name
Table:    Employee
max. Len: 50
Flags:    1
Type:     254

Name:     Age
Table:    Employee
max. Len: 10
Flags:    32769
Type:     4

❮ PHP MySQLi Reference