English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_fetch_field_direct() function usage and example

   PHP MySQLi Reference Manual

The mysqli_fetch_field_direct() function retrieves metadata for a single field

Definition and usage

PHP result object (mysqli_result class) represents MySQL result, returned by SELECT or DESCRIBE or EXPLAIN query.
The mysqli_fetch_field_direct() function accepts a result object and an integer representing the field number, and returns the specified column in the form of an object./Field definition information.

Syntax

mysqli_fetch_field_direct($result, $field);

Parameter

Serial numberParameters and descriptions
1

result(Required)

This is the identifier for the result object.

2

field(Required)

An integer value representing the metadata you need/Fields with definition information.

Return value

The PHP mysqli_fetch_field_direct() function returns an object that contains the definition information of the specified field. If the specified field (number) is not available, this function returnsFALSE.

The returned object contains the following properties:

  • name - Column name

  • orgname - Original column name (if an alias is specified)

  • table - Table name

  • orgtable - Original table name (if an alias is specified)

  • def - Default value of this field

  • max_length - Maximum field width

  • length - Field width specified in table definition

  • charsetnr - Field character set number

  • flags - Bit flags of the field

  • type - Data type used for the field

  • decimals - integer field, number of decimal places after the decimal point

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following example demonstratesmysqli_fetch_field_direct()Usage of the function (procedural style), returns the metadata of the third field (column) in the result set and outputs the field name, table, maximum length, data type, and character set number of the field:

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Create table.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("Inserting records.....\n");
   //Retrieve the content of the table
   $res = mysqli_query($con, "SELECT * FROM myplayers");
   //Get the3metadata of a field
   $info = mysqli_fetch_field_direct($res, 2);
   print("Name: "$info");->name."\n");
   print("Table: "$info");->table."\n");
   print("Max Length: "$info");->max_length."\n");
   print("Flags: "$info");->flags."\n");
   print("Type: "$info");->type."\n");
   print("Definition: "$info");->def."\n");
   print("Character Set: "$info");->charsetnr."\n");
   //End statement
   mysqli_free_result($res);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
Inserting records.....
Name: Last_Name
Table: myplayers
Max Length: 10
Flags: 0
Type: 253

Online Example

The syntax of this function in the object-oriented style is:$result->fetch_field_direct();。This function, in the object-oriented style, returns the metadata of a second field (column) in the result set and outputs the field name, table, maximum length, data type, and character set number of the field:

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27);
   print("Create table.....\n");
   $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   //Execute statement
   $stmt->execute();
   //Retrieve result
   $result = $stmt->get_result();
   //Get metadata of the second field
   $info = $result->fetch_field_direct(1);
   print("Name: "$info");->name."\n");
   print("Table: "$info");->table."\n");
   print("Max Length: "$info");->max_length."\n");
   print("Flags: "$info");->flags."\n");
   print("Type: "$info");->type."\n");
   print("Definition: "$info");->def."\n");
   print("Character Set: "$info");->charsetnr."\n");
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Name: AGE
Table: Test
Max Length: 0
Flags: 32768
Type: 3
Definition:
Character Set: 63

PHP MySQLi Reference Manual