mysqli_result fetch_fields() Method
The mysqli_result::fetch_fields() / mysqli_fetch_fields() function returns an array of objects representing the fields in a result set.
This function is similar to mysqli_fetch_field() function, except instead of returning one object at a time for each field, the columns are returned as an array of objects.
Syntax
//Object-oriented style public mysqli_result::fetch_fields() //Procedural style mysqli_fetch_fields(result)
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(). |
Return Value
Returns an array of objects containing field definition information.
Object properties
Property | 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 |
max_length | The maximum width of the field for the result set |
length | The width of the field, in bytes, as specified in the table definition. Note that this number (bytes) might differ from your table definition value (characters), depending on the character set used. For example, the character set utf8 has 3 bytes per character, so varchar(10) will return a length of 30 for utf8 (10*3), but return 10 for latin1 (10*1). |
charsetnr | The character set number (id) 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 integer fields) |
Example: Object-oriented style
The example below shows the usage of mysqli_result::fetch_fields() 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 all columns $finfo = $result->fetch_fields(); foreach ($finfo as $val) { printf("Name: %s\n", $val->name); printf("Table: %s\n", $val->table); printf("Max. Len: %d\n\n", $val->max_length); } //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 Name: Age Table: Employee Max. Len: 10
Example: Procedural style
The example below shows the usage of mysqli_fetch_fields() 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 all columns $finfo = mysqli_fetch_fields($result); foreach ($finfo as $val) { printf("Name: %s\n", $val->name); printf("Table: %s\n", $val->table); printf("Max. Len: %d\n\n", $val->max_length); } //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 Name: Age Table: Employee Max. Len: 10
❮ MySQLi Functions Reference