fetch_field_direct() returns metadata for the field at the specified zero-based position.
<?php
$result = $connection->query('SELECT id, name, mark FROM student LIMIT 1');
$field = $result->fetch_field_direct(1);
echo $field->name;
?>Current example first; the detailed examples below are retained for additional learning and comparison.
<?php
require "config.php";// Database connection file.
if ($result = $connection->query("SELECT * FROM student")) {
$field_details = $result->fetch_field_direct(0);
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;
$result->close();
}
?>
Name name
Data Type 253
Max Length 11
Flags 1
Table student
$result_set->fetch_field_direct ($field_no);
mysqli_fetch_field_direct($result_set,$field_no)
The input parameter $result_set is the output of result set identifier of mysqli_query(), mysqli_store_result() or mysqli_use_result().
MySQLI database connection file
Procedural style<?php
require "config.php";// Database connection file.
if ($result_set = mysqli_query($connection,$query)) {
$field_details=mysqli_fetch_field_direct($result_set,1);
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;
}
?>
Data type number => name
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
//252 is currently mapped to all text and blob types (MySQL 5.0.51a)
253=>'varchar',
254=>'char',
246=>'decimal'
mysqli_fetch_field() to get details of all Columns
MYSQLI Functions mysqli_num_rows() Number of rows in result set SELECT query UPDATE query mysqli_fetch_lengths() Data Field LengthAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.