mysql_field_name() returned the name of a result-set column from the removed PHP MySQL extension. Current code can obtain column names from the database schema or from result metadata.
<?php
// Historical only.
$q = mysql_query('SELECT * FROM student ORDER BY id');
$name = mysql_field_name($q, 0);
echo $name;
?>
SHOW COLUMNS FROM student;
<?php
$columns = $dbo->query('SHOW COLUMNS FROM student');
echo '<tr>';
foreach ($columns as $column) {
echo '<th>' . htmlspecialchars($column['Field']) . '</th>';
}
echo '</tr>';
?>
<?php
$stmt = $dbo->query(
'SELECT id, name, class, mark FROM student'
);
echo 'Number of columns: ' . $stmt->columnCount();
?>
<?php
$result = $connection->query(
'SELECT id, name, class, mark FROM student ORDER BY id'
);
echo '<tr>';
foreach ($result->fetch_fields() as $field) {
echo '<th>' . htmlspecialchars($field->name) . '</th>';
}
echo '</tr>';
?>
The original goal—using field names as table headings—is preserved without mysql_num_fields(), mysql_fetch_row(), or PHP's removed each() function.
Number of fields · Column metadata · Field types · Student SQL dump · MySQLi overview
Author & 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.