mysqli_fetch_field(): Read Field Metadata

Loop through result-field metadata

fetch_field() returns the next field-definition object each time it is called.

<?php
$result = $connection->query('SELECT id, name, mark FROM student LIMIT 1');

while ($field = $result->fetch_field()) {
    echo $field->name . '<br>';
}
?>

Current example first; the detailed examples below are retained for additional learning and comparison.

<?php
require "config.php";// Database connection file.

$query="select * from student ";
if ($result_set = mysqli_query($connection,$query)) {
while ($field_details = mysqli_fetch_field($result_set)) {

echo "<br> <b>Name</b>  ".$field_details->name;
echo "<br> <b>Data Type</b>  ".$field_details->type;
echo "<br> <b>Max Length</b>  ".$field_details->max_length;
echo "<br> <b>Length</b>  ".$field_details->length;
echo "<br> <b>Flags</b>  ".$field_details->flags;
echo "<br> <b>Table</b>  ".$field_details->table;
echo "<br><br>";    
}
}
?>
Name id
Data Type 3
Max Length 2
Flags 32769
Table student

Name name
Data Type 253
Max Length 11
Flags 1
Table student

Name class
Data Type 253
Max Length 5
Flags 1
Table student

Name mark
Data Type 3
Max Length 3
Flags 32769
Table student

Name sex
Data Type 253
Max Length 6
Flags 1
Table student
$result_set->fetch_field;
mysqli_fetch_field
The input parameter $result_set is the output of result set identifier of mysqli_query(), mysqli_store_result() or mysqli_use_result().

The output is an object array containing all field defination information, returns FALSE if not available.

MySQLI database connection file

MySQL SELECT Query

Procedural style
<?php
require "config.php";// Database connection file.

if ($result = $connection->query("SELECT * FROM student")) {
    
while ($field_details = $result->fetch_field()) {
echo "<br> <b>Name</b>  ".$field_details->name;
echo "<br> <b>Data Type</b>  ".$field_details->type;
echo "<br> <b>Max Length</b>  ".$field_details->max_length;
echo "<br> <b>Flags</b>  ".$field_details->flags;
echo "<br> <b>Table</b>  ".$field_details->table;
echo "<br><br>";
}
 $result->close();
}
?>

MySQL DUMP of student table

MYSQLI Functions mysqli_num_rows() Number of rows in result set SELECT query UPDATE query mysqli_fetch_field_direct(): Field meta data


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer