mysqli_result fetch_field_direct() Method
The mysqli_result::fetch_field_direct() / mysqli_fetch_field_direct() function fetches meta-data for a single field. It returns an object which contains field definition information from the specified result set.
Syntax
//Object-oriented style public mysqli_result::fetch_field_direct(index) //Procedural style mysqli_fetch_field_direct(result, index)
Parameters
result |
Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). |
index |
Required. Specify the field number. This value must be an integer in the range from 0 to (number of fields - 1). |
Return Value
Returns an object which contains field definition information or false if no field information is available.
Object attributes
Attribute | Description |
---|---|
name | The name of the column |
orgname | Original column name if an alias was specified |
table | The name of the table this field belongs to (if not calculated) |
orgtable | Original table name if an alias was specified |
def | The default value for this field, represented as a string |
max_length | The maximum width of the field for the result set |
length | The width of the field, as specified in the table definition |
charsetnr | The character set number for the field |
flags | An integer representing the bit-flags for the field |
type | The data type used for this field |
decimals | The number of decimals used (for number fields) |
Example: Object-oriented style
The example below shows the usage of mysqli_result::fetch_field_direct() 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(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; if ($result = $mysqli->query($sql)) { //getting field information for column 'Age' $finfo = $result->fetch_field_direct(1); 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", $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: Age Table: Employee max. Len: 10 Flags: 32769 Type: 4
Example: Procedural style
The example below shows the usage of mysqli_fetch_field_direct() 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(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; if ($result = mysqli_query($mysqli, $sql)) { //getting field information for column 'Age' $finfo = mysqli_fetch_field_direct($result, 1); 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", $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: Age Table: Employee max. Len: 10 Flags: 32769 Type: 4
❮ MySQLi Functions Reference