mysqli_fetch_fields() - Fetch Field Information

This section provides a tutorial example on how to fetch field information from query result by calling the mysqli_fetch_fields() function.

If you want to know the header names of columns in the query result object, you can use the mysqli_fetch_fields() function:

$fields = mysqli_fetch_fields($res);

where:
  $res - The SELECT query result object
  $fields - Array of field information objects 

Here is a PHP script example showing you how to use the mysqli_fetch_row() function:

<?php
#  mysqli-fetch-fields.php
#- Copyright 2009-2026 (c) HerongYang.com. All Rights Reserved.

  $con = mysqli_connect("127.0.0.1", "herong", "TopSecret", "MyBase");
  $res = mysqli_query($con, 
    "CREATE TABLE MyTable ".
    "(ID INTEGER AUTO_INCREMENT, Value INTEGER, PRIMARY KEY (ID))");

  # query the table structure 
  $res = mysqli_query($con, "DESC MyTable");

  # display field names
  $fields = mysqli_fetch_fields($res);
  foreach ($fields as $field) {
    print(str_pad($field->name, 10));
  }
  print("\n");

  # display field types
  foreach ($fields as $field) {
    print(str_pad($field->type, 10));
  }
  print("\n");

  # display field lengths
  foreach ($fields as $field) {
    print(str_pad($field->length, 10));
  }
  print("\n");

  # display dash bars
  foreach ($fields as $field) {
    print(str_pad("------", 10));
  }
  print("\n");

  # display field values
  while ($row = mysqli_fetch_row($res)) {
    foreach ($row as $val) {
      print(str_pad($val, 10));
    }
    print("\n");
  }
  mysqli_free_result($res);

  mysqli_close($con);
?>

If you run the script, you will get the following output:

herong$ php mysqli-fetch-fields.php 

Field     Type      Null      Key       Default   Extra     
253       252       253       254       252       253       
192       50331645  9         9         196605    768       
------    ------    ------    ------    ------    ------    
ID        int(11)   NO        PRI                 auto_increment
Value     int(11)   YES                                     

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Environment and Modules on macOS

 Managing PHP Environment and Modules on CentOS

 Configuration and Modules

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

 GD Module - Manipulating Images and Pictures

MySQLi Module - Accessing MySQL Server

 MySQLi Module and Configuration

 mysqli-hello.php - Test MySQLi Module

 mysqli_connect() - Open MySQL Connection

 mysqli_query() - Run SQL Queries

 mysqli_affected_rows() - Count Affected Rows

 mysqli_insert_id() - ID from AUTO_INCREMENT

 mysqli_fetch_array() - Fetch Row from Query Result

mysqli_fetch_fields() - Fetch Field Information

 Escape Escape Characters - \\\\ for \\

 Summary of Basic MySQLi Functions

 MySQLi Object-Oriented Programming

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

 Zip Module - Managing ZIP Archive Files

 References

 Index

 Full Version in PDF/EPUB